iOS解决UITableView中Cell重用带来的问题

tableView的常规配置,当超出一屏的cell就会标上可重用的标识出列到可重用缓存池中,后面再根据可重用标识来到的可重的cell就会和前面显示同样内容。

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

    // 定义唯一标识

    static NSString *CellIdentifier = @"Cell";

    // 通过唯一标识创建cell实例

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    // 判断为空进行初始化  --(当拉动页面显示超过主页面内容的时候就会重用之前的cell,而不会再次初始化)

    if (!cell) {

        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

    }

    // 对cell 进行简单地数据配置

    cell.textLabel.text = @"text";

         cell.detailTextLabel.text = @"text";

    cell.imageView.image = [UIImage imageNamed:@"4.png"];

    return cell;

}

 

 

方法一: 取消cell的重用机制,通过indexPath来创建cell 将可以解决重复显示问题 不过这样做相对于大数据来说内存就比较吃紧了,通过不让它重用cell,来解决重复显示

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

    // 定义唯一标识

    static NSString *CellIdentifier = @"Cell";

    // 通过indexPath创建cell实例 每一个cell都是单独的

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    // 判断为空进行初始化  --(当拉动页面显示超过主页面内容的时候就会重用之前的cell,而不会再次初始化)

    if (!cell) {

        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

    }

    // 对cell 进行简单地数据配置

    cell.textLabel.text = @"text";

    cell.detailTextLabel.text = @"text";

    cell.imageView.image = [UIImage imageNamed:@"4.png"];

    return cell;

}

 

 

方法二: 让每个cell都拥有一个对应的标识 这样做也会让cell无法重用 所以也就不会是重复显示了 显示内容比较多时内存占用也是比较多的和方案一类似,同样通过不让它重用cell来解决重复显示,不同的每个cell对应一个重用标识

 

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

    

    // 定义cell标识  每个cell对应一个自己的标识

    

    NSString *CellIdentifier = [NSString stringWithFormat:@"cell%ld%ld",indexPath.section,indexPath.row];

    

    // 通过不同标识创建cell实例

    

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    

    // 判断为空进行初始化  --(当拉动页面显示超过主页面内容的时候就会重用之前的cell,而不会再次初始化)

    

    if (!cell) {

        

        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

        

    }

    

    // 对cell 进行简单地数据配置

    

    cell.textLabel.text = @"text";

    

    cell.detailTextLabel.text = @"text";

    

    cell.imageView.image = [UIImage imageNamed:@"4.png"];

    

    return cell;

}

 

 

方法三: 只要最后一个显示的cell内容不为空,然后把它的子视图全部删除,等同于把这个cell单独出来了 然后跟新数据就可以解决重复显示,同样通过不让他重用cell来解决重复显示,不同的每个cell对应一个标识。

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

    // 定义唯一标识

    static NSString *CellIdentifier = @"Cell";

    // 通过唯一标识创建cell实例

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    // 判断为空进行初始化  --(当拉动页面显示超过主页面内容的时候就会重用之前的cell,而不会再次初始化)

    if (!cell) {

        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

    }else{

        //当页面拉动的时候 当cell存在并且最后一个存在 把它进行删除就出来一个独特的cell我们在进行数据配置即可避免

        while ([cell.contentView.subviews lastObject] != nil) {

            [(UIView *)[cell.contentView.subviews lastObject] removeFromSuperview];

        }

    }

    // 对cell 进行简单地数据配置

    cell.textLabel.text = @"text";

    cell.detailTextLabel.text = @"text";

    cell.imageView.image = [UIImage imageNamed:@"4.png"];    

    return cell;

}

 

转载于:https://www.cnblogs.com/chenweb/p/8743788.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值