详解iOS开发中UItableview控件的数据刷新功能的实现

2020-01-14 17:46:37王振洲

    self.tableview.dataSource=self;
    self.tableview.delegate=self;
    self.tableview.rowHeight=60.f;
    NSLog(@"%d",self.heros.count);
}

#pragma mark -懒加载
-(NSArray *)heros
{
    if (_heros==nil) {
        
        NSString *fullpath=[[NSBundle mainBundle]pathForResource:@"heros.plist" ofType:nil];
        NSArray *temparray=[NSArray arrayWithContentsOfFile:fullpath];
        
        NSMutableArray *arrayM=[NSMutableArray array];
        for (NSDictionary *dict in temparray) {
            YYheros *hero=[YYheros herosWithDict:dict];
            [arrayM addObject:hero];
        }
        _heros=[arrayM mutableCopy];
    }
    return _heros;
}

#pragma mark- tableview的处理
//多少组
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
//多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.heros.count;
}
//每组每行的数据,设置cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //NSLog(@"cellForRowAtIndexPath 修改的了 %d", indexPath.row);
    //1.去缓存中取
    static NSString *identifier=@"hero";
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
    //2.如果没有,那么就自己创建
    if (cell==nil) {
        NSLog(@"chuangjiancell");
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    //3.设置数据
    
    //3.1拿到该行的模型
    YYheros *hero=self.heros[indexPath.row];
    cell.textLabel.text=hero.name;
    cell.imageView.image=[UIImage imageNamed:hero.icon];