解决UITableView上的cell的重用

UITableViewCell重用机制
本文介绍了一种通过设置不同重用标识符解决UITableViewCell重用问题的方法,并提供了清除重用cell所有子视图的具体实现,以便更好地管理和重用UITableViewCell。

1.通过为每个cell指定不同的重用标识符(reuseIdentifier)来解决

//        static NSString *rankCellIndefier = @"rankCell";
        NSString *cellMark = [NSString stringWithFormat:@"%ld", indexPath.row];
        
        NewsCustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellMark];
        tableView.rowHeight = 80;
        if (cell == nil) {
            cell = [[[NewsCustomTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellMark] autorelease];
           if (indexPath.row < 3) {
                  UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 20, 20)];
                imageView.tag = 100 + indexPath.row;
                [cell addSubview:imageView];
               [imageView release];
           }
        }
        if (indexPath.row < 3) {
            UIImageView *imageView = (UIImageView *)[cell viewWithTag:100 + indexPath.row];
            imageView.image = [UIImage imageNamed:rankingArray[indexPath.row]];
        }
        cell.titleLabel.text = news.headLineTitle;
        [cell resetLabelFrame:news.headLineTitle];
        cell.dateLabel.text = news.headLineDate;
        cell.rightCountLabel.text = news.headLineCount;
        [pictureAsy pictureCustom:cell imageUrl:news.headLinePhoto indexImage:0];
        return cell;  
    }

删除重用cell的所有子视图

这个方法是通过删除重用的cell的所有子视图,从而得到一个没有特殊格式的cell,供其他cell重用。

 

  1. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  2. {  
  3.     static NSString *CellIdentifier = @"Cell";  
  4.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //出列可重用的cell  
  5.     if (cell == nil) {  
  6.         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];  
  7.     }  
  8.     else  
  9.     {  
  10.         //删除cell的所有子视图  
  11.         while ([cell.contentView.subviews lastObject] != nil)  
  12.         {  
  13.             [(UIView*)[cell.contentView.subviews lastObject] removeFromSuperview];  
  14.         }  
  15.     }  
  16.     //...其他代码  
  17. }

 

 

 

 

转载于:https://www.cnblogs.com/tian-sun/p/4220533.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值