iOS中使用UItableviewcell实现团购和微博界面的示例

2020-01-14 18:26:55于海丽

    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell==nil) {
       // cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell= [[[NSBundle mainBundle]loadNibNamed:@"tgcell" owner:nil options:nil] firstObject];
    }
    
    YYtg *tg=self.tg[indexPath.row];
    //设置数据
    //使用tag
    UIImageView *imgv=(UIImageView *)[cell viewWithTag:1];
    imgv.image=[UIImage imageNamed:tg.icon];
    UILabel *buyCount=(UILabel *)[cell viewWithTag:4];
    buyCount.text=[NSString stringWithFormat:@"已有%@人购买",tg.buyCount];
    UILabel *title=(UILabel *)[cell viewWithTag:2];
    title.text=tg.title;
    UILabel *price=(UILabel *)[cell viewWithTag:3];
    price.text=[NSString stringWithFormat:@"$%@",tg.price];
    
    
    //返回cell
    return cell;
}

//隐藏状态栏
-(BOOL)prefersStatusBarHidden
{
    return YES;
}
@end


使用xib自定义的UItableviewcell

 

iOS中使用UItableviewcell实现团购和微博界面的示例

代码分析:

上面的代码通过使用xib文件中各个控件的tag值,完成对每个部分数据的赋值和刷新。但是,作为主控制器,它应该知道xib文件中各个控件的tag值,它知道的是不是太多了呢?

为了解决上面的问题,我们可以为自定义的cell设置一个配套的类,让这个类来操作这个xib,对外提供接口,至于内部的数据处理,外界不需要关心,也不用关心。

改造后的代码如下:

 

2.使用xib和对应的类完成自定义cell的数据展示

新建一个类,用来管理对应的xib文件