`
ydbc
  • 浏览: 718582 次
  • 性别: Icon_minigender_1
  • 来自: 大连
文章分类
社区版块
存档分类
最新评论

关于UItabView Cell 自定义重用的 代码

 
阅读更多

cocoa 默认的cell风格修改起来挺灵活的 先提供处自定义代码 其实难点在于cell重用机制 供初学者参考

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

{

static NSString * showUserInfoCellIdentifier =@"ShowUserInfoCell";

UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:showUserInfoCellIdentifier];

if (cell == nil)

{

// Create a cell to display an ingredient.

cell = [[[UITableViewCellalloc] initWithStyle:UITableViewCellStyleSubtitle

reuseIdentifier:showUserInfoCellIdentifier]

autorelease];

UIImageView *leftico= [[[UIImageViewalloc]init ]autorelease];

leftico.tag = 11;

leftico.frame=CGRectMake(0,0, 100, 60);

[leftico setContentMode:UIViewContentModeScaleAspectFit];

UILabel *titles= [[[UILabelalloc] initWithFrame:CGRectMake(110,0,120,60)] autorelease];

[titles setBackgroundColor:[UIColorclearColor] ];

titles.tag=22;

[cell addSubview:titles];

[cell addSubview:leftico];

cell.accessoryType =UITableViewCellAccessoryDetailDisclosureButton;//添加其默认的细节按钮

}

NSUInteger row=[indexPath row];

NSLog(@"name == %@",[ [self.listdataobjectAtIndex:row] objectForKey:@"name"]);

UIImageView*imageView11 = (UIImageView *)[cellviewWithTag:11]; //重新指向那片内存

//[ [cell viewWithTag:1] removeFromSuperview];

imageView11.image = [UIImageimageNamed:@"gongshang.png"];

UILabel *titles22= (UILabel *)[cellviewWithTag:22];

titles22.text=[ [self.listdataobjectAtIndex:row] objectForKey:@"name"];

// if (cell.textLabel.text isEqualToString:@"工商银行") {

// cell.imageView.image= [ UIImage imageNamed:@"bg.jpg" ]

// }

return cell;

}



下面有一网友做的例程 分析对比下 看看有什么收获

解决自定义UITableViewCell在浏览中出现数据行重复的问题
2010-12-27 10:52:22
原创作品,允许转载,转载时请务必以超链接形式标明文章原始出处、作者信息和本声明。否则将追究法律责任。http://ddkangfu.blog.51cto.com/311989/465557

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

  1. -(UITableViewCell*)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(NSIndexPath*)indexPath
  2. {
  3. NSIntegersection=[indexPathsection];
  4. NSIntegerrow=[indexPathrow];
  5. UITableViewCell*cell;
  6. switch(section)
  7. {
  8. case0:
  9. //dosomething.
  10. case1:
  11. cell=[tableViewdequeueReusableCellWithIdentifier:@"Cell"];
  12. if(cell==nil)
  13. {
  14. cell=[[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:@"Cell"]autorelease];
  15. //Image
  16. UIImageView*image=[[UIImageViewalloc]initWithFrame:CGRectMake(0.0f,14.0f,45.0f,50.0f)];
  17. image.backgroundColor=[UIColorclearColor];
  18. image.image=[UIImageimageNamed:@"folder.png"];
  19. [cell.contentViewaddSubview:image];
  20. [imagerelease];
  21. //Label
  22. UILabel*titleLabel=[[UILabelalloc]initWithFrame:CGRectMake(45.0f,6.0f,214.0f,50.0f)];
  23. titleLabel.text=(NSString*)[(NSArray*)[self.categoryArrayobjectAtIndex:1]objectAtIndex:row];
  24. NSLog(@"%@--%d",titleLabel.text,row);
  25. titleLabel.textAlignment=UITextAlignmentLeft;
  26. titleLabel.numberOfLines=3;
  27. titleLabel.tag=201;
  28. titleLabel.font=[UIFontboldSystemFontOfSize:14];
  29. [cell.contentViewaddSubview:titleLabel];
  30. [titleLabelrelease];
  31. }
  32. cell.accessoryType=UITableViewCellAccessoryDetailDisclosureButton;
  33. break;
  34. }
  35. cell.selectionStyle=UITableViewCellSelectionStyleNone;
  36. returncell;
  37. }

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

  1. cell=[tableViewdequeueReusableCellWithIdentifier:@"Cell"];

替换成

  1. cell=[tableViewcellForRowAtIndexPath:indexPath];

  1. cell=nil;

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

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

  1. //在构造函数里定义cell数组
  2. for(inti=0;i<31;i++)
  3. {
  4. staticNSString*MyBookMarkIdentifier=@"CityMangerCell";
  5. cityCell[i]=[[CityMangerCellalloc]initWithFrame:CGRectZeroreuseIdentifier:MyBookMarkIdentifierinitIndex:i];
  6. }
  7. //使用它
  8. -(UITableViewCell*)tableView:(UITableView*)tableViewcellForRowAtIndexPath:(NSIndexPath*)indexPath
  9. {
  10. if((0<=indexPath.row)&&(indexPath.row<31))
  11. returncityCell[indexPath.row];
  12. returnnil;
  13. }

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

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

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

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

  1. if(cell==nil)
  2. {
  3. ....
  4. }
  5. UILabel*l1=(UILabel*)[cell.contentViewviewWithTag:201];
  6. l1.text=(NSString*)[(NSArray*)[self.categoryArrayobjectAtIndex: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内容重复的现象。
希望这个问题的解决过程会对大家有所帮助。

本文出自 “一叶障目” 博客,请务必保留此出处http://ddkangfu.blog.51cto.com/311989/465557


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics