UITableViewCell复用重叠的问题

更多iOS 学习知识,总结尽在  的墨科技:传送门


当用到cell重用时 会出现以下问题:只要cell重用了,内容就会覆盖叠加

当cell重用时,就出现了以上问题,叠加


解决办法

一、使用Xib, 进行cell的关联
在controller里注册xib

[_listTableView  registerNib:[UINib nibWithNibName:@"InformationCell" bundle:nil] forCellReuseIdentifier:@"Infor"];


- (InformationCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString * cellIndentifier = @"Infor";

    InformationCell *cell = (InformationCell*)[tableView  dequeueReusableCellWithIdentifier:cellIndentifier forIndexPath:indexPath];

    if (  cell == nil ) {

        cell = [[InformationCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier];

        cell.backgroundColor = [UIColor clearColor];

        cell.contentView.backgroundColor = [UIColor clearColor];

        cell.selectionStyle = UITableViewCellSelectionStyleNone;

    }

    

    MessageModel * model = [_messArray objectAtIndex:indexPath.row];

    if ( [model isKindOfClass:[MessageModel class]] ) {

        [cell loadData:model];

    }

    

    return cell;

}



二、使用cell纯代码,注册cell 

[_listTableView registerClass:[ActivityTableViewCell class] forCellReuseIdentifier:@"activityCell"]; // activityCell 自定义的名称

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

       ActivityTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"activityCell"];

    if (!cell) {

         cell = [[ActivityTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"activityCell"];

    }

    

    ActivityModel *model = [_array objectAtIndex:indexPath.section];

    cell.activityModel = model;

    cell.selectionStyle=UITableViewCellSelectionStyleNone;

    return cell;

}


关键的一步

cell.m进行创建UILayout时,初始化cell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier

{

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

    if (self) {

        // Initialization code

        [self createActivityUI];

    }

    return self;

}


//- (void)setSelected:(BOOL)selected animated:(BOOL)animated {

//    [super setSelected:selected animated:animated];

//

//    [self createActivityUI];

//}


应该使用未注释的代码,进行创建,会正常显示


如果使用了注释的代码,即会产生cell的重用叠加错误




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿三先生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值