IOS-UITableView-Cell的重绘-避免出现重叠视图

在IOS6.0以后,UITableView新增了两个方法

用于Xib的
- (void)registerNib:(UINib *)nib forCellReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(5_0); 

用于手写的
- (void)registerClass:(Class)cellClass forCellReuseIdentifier:(NSString *)identifier NS_AVAILABLE_IOS(6_0);

很多书上的方法,关于UITableView的重绘方法,都是这么写的

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:CellIdentifier];
        //设置你的cell
}
这样写用在自定义的Cell上就会出现重复的问题

避免这个方法的解决方案就是在创建UITableView的时候,直接register一个Cell

[self.tableView registerClass:[CustomCell class] forCellReuseIdentifier:@"CustomCell"];
用纯代码的就使用registerClass,用Xib的就是用registerNib
然后在 (UITableViewCell  *)tableView:(UITableView  *)tableView  cellForRowAtIndexPath:(NSIndexPath  *)indexPath这个方法里

直接使用

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
关于自定义Cell,我们可以先初始化UI,然后设置一个Model,在Model再进行数据的处理,以下是我的自定义Cell的写法

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupView];//先创建UI
    }
    return self;
}
- (void)setModel:(getChatModel *)model  //处理数据
{
    [self.headImageView setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:IMG_BASE_HOST,    model.fdUserHeaderUrl]] placeholderImage:[UIImage imageNamed:@"img_user_header_cache.jpg"] usingAc    tivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    [self.headImageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self actio    n:@selector(act_headimg)]];
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值