解决自定义UITableViewCell在浏览中出现数据行重复的问题

我在写一个App的时候自定义了一个UITableViewCell,但是这个UITableView在运行的时候出现了每6行数据就循环重复显示的问题,而直接使用cell.textLabel.text显示是没有这个问题,以下是我实现的代码。

 
 
  1. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  2.     NSInteger section = [indexPath section]; 
  3.     NSInteger row = [indexPath row]; 
  4.     UITableViewCell *cell; 
  5.  
  6.     switch (section) 
  7.     { 
  8.         case 0: 
  9.       //do something. 
  10.         case 1: 
  11.             cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; 
  12.             if (cell == nil) 
  13.             { 
  14.                 cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"] autorelease]; 
  15.  
  16.                 //Image 
  17.                 UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 14.0f, 45.0f, 50.0f)]; 
  18.                 image.backgroundColor = [UIColor clearColor]; 
  19.                 image.image = [UIImage imageNamed:@"folder.png"]; 
  20.                 [cell.contentView addSubview:image]; 
  21.                 [image release]; 
  22.                 //Label 
  23.                 UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(45.0f, 6.0f, 214.0f, 50.0f)]; 
  24.                 titleLabel.text = (NSString *)[(NSArray *)[self.categoryArray objectAtIndex:1] objectAtIndex:row]; 
  25.                 NSLog(@"%@ -- %d", titleLabel.text, row); 
  26.                 titleLabel.textAlignment = UITextAlignmentLeft; 
  27.                 titleLabel.numberOfLines = 3; 
  28.                 titleLabel.tag = 201; 
  29.                 titleLabel.font = [UIFont boldSystemFontOfSize:14]; 
  30.                 [cell.contentView addSubview:titleLabel]; 
  31.                 [titleLabel release]; 
  32.             } 
  33.             cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton; 
  34.             break
  35.     } 
  36.  
  37.     cell.selectionStyle = UITableViewCellSelectionStyleNone; 
  38.     return cell; 

google了一下,目前已有的解决方案是将

 
 
  1. cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; 

替换成

 
 
  1. cell = [tableView cellForRowAtIndexPath:indexPath]; 

 
 
  1. cell = nil;

这们做的目的去掉Cell的重用机制,但是这种方法都会在后台随着表格滚动一直在创建cell,通过上面源代码中Label定义里那句NSLog在控制台输出就可以看到,虽然会自动回收内存,但肯定也会给系统带来不小开销,所以不到万一得以还是不会用的。

还有一种解决方案是自己定义Cell数组,在tableView:tableView cellForRowAtIndexPath:中进设置要显示的cell,这是手工维护cell的一种方式,对大数据量的情况肯定是不适用的,不过也能算得上是一种思路吧,可以参考一下。其代码如下:

 
 
  1. //在构造函数里定义cell数组 
  2. for(int i = 0; i < 31; i ++)                                                                                      
  3. {                                                                                                                 
  4.     static NSString *MyBookMarkIdentifier = @"CityMangerCell";                                                        
  5.     cityCell[i] = [[CityMangerCell alloc] initWithFrame:CGRectZero reuseIdentifier:MyBookMarkIdentifier initIndex:i]; 
  6. }                                                                                                                 
  7.  
  8. //使用它 
  9. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
  10. {                                                                                                      
  11.     if((0 <= indexPath.row) && (indexPath.row < 31))                                                       
  12.      return cityCell[indexPath.row];                                                                     
  13.     return nil;                                                                                            

后来我仔细分析了一下程序,找到了问题所在:

原因是在if (cell == nil)判断内部不应该对其label进行赋值,即不使用这句:

 
 
  1. titleLabel.text = (NSString *)[(NSArray *)[self.categoryArray objectAtIndex:1] objectAtIndex:row]; 

正确的做法应该是在if (cell == nil){}判断后面进行赋值。即

 
 
  1. if (cell == nil)                                                                           
  2. {                                                                                          
  3.     ....                                                                                   
  4. }                                                                                          
  5. UILabel *l1 = (UILabel *)[cell.contentView viewWithTag: 201];                              
  6. l1.text = (NSString *)[(NSArray *)[self.categoryArray objectAtIndex:1] objectAtIndex:row]; 

分析原因如下:
UITableView中被实例化的cell个数由屏高和每个cell的高度决定,因为我的cell高度设置为80,一屏只能 显示6个Cell(只有6个cell被实例化),也就是只有这6个cell才会执行if (cell == nil){}中的代码,从第6行往后的cell都是重用的这6个cell,也就是说从第7行开始将不会执行if (cell = nil){}中的代码,当UITableView需要绘制第7行cell的时候,会取得第1个cell进行重用,如果我们不把原来第1行cell中的 Label内容进行修改,那么第7行将完全显示第1行中的内容,所以才会在第6行之后开始出现数据重复的情况。
现在我将Label内容设置的代码放到if (cell == nil){}之后,它将会对每一个被重用的cell的Label进行设定,也就不会再出现cell内容重复的现象。
希望这个问题的解决过程会对大家有所帮助。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值