折叠分组表格中重用Cell导致的问题

来源:互联网 发布:淘宝装修代码运行不了 编辑:程序博客网 时间:2024/05/24 01:45

问题描述:

在使用UITableView时,我们习惯在tableView:cellForRowAtIndexPath:方法中使用CocoaTouch提供的表格单元格缓存来重用单元格:

static NSString* cellid=@"exTable_cellid";

UITableViewCell* cell=(UITableViewCell*)[tableView

dequeueReusableCellWithIdentifier:cellid];

if (cell==nil) {

cell=[[[UITableViewCell alloc]

initWithStyle:UITableViewCellStyleDefault

reuseIdentifier:cellid]autorelease];

UILabel *lblName[[UILabel alloc]initWithFrame:

 CGRectMake(20, 6, 100, 20)];

……

 

}

表面上看这没有任何问题。但对于可折叠分组的表格,则会导致一系列问题。原因在于Cocoa Touch提供的单元格缓存无法识别不同的组(section),对它来说,第1个section的第1行,和第2个、第3个…第n个section的第1行都是一样的。于是,当你打开了第1个section后,再打开其他的section,会发现,所有的section的前面几行全都变成了第1个section中的内容,例如在一个折叠分组表格中,我们先打开一个section:

然后再打开其他的section,发现,所有的section下的第1行内容都变成了zhangsz:

既然问题提出了,让我们来看看解决的方法。

解决办法:

解决的办法有两个,一是不使用Cocoa Touch的单元格缓存,即每次都重新构建UITableViewCell:

//这里不采用重用方式,每次刷新都重新构造cell。对于分组表格,重用cell会导致一些问题

// static NSString*cellid=@"exTable_cellid";

UITableViewCell* cell=[[UITableViewCell alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width,rowHeight)];

UILabel *lblName[[UILabel alloc]initWithFrame:

 CGRectMake(20, 6, 100, 20)];

……

这里我们采用了UITableViewCell的另外一个初始化方法来构建单元格,这个方法不需要传递reuseIndentifier参数,即不使用单元格缓存。这种方法简单有效,但由于每次刷新表格时都会重新构造单元格,与重用单元格缓存比较而言,会导致额外的性能开销。

第二种方法,是使用tag属性和viewWithTag方法。这种情况下可以使用单元格缓存,同时避免了前面所述问题的出现:

static NSString* cellid=@"exTable_cellid";

UITableViewCell* cell=(UITableViewCell*)[tableView

dequeueReusableCellWithIdentifier:cellid];

if (cell==nil) {

cell=[[[UITableViewCell alloc]

initWithStyle:UITableViewCellStyleDefault

reuseIdentifier:cellid]autorelease];

UILabel *lblName[[UILabel alloc]initWithFrame:

 CGRectMake(20, 6, 100, 20)];

lblName.tag=1022;

[cell addSubView:lblName];

……

}

lblName=[cellviewWithTag:1022];

lblName.text=… …

… …

 

聪明的你,是不是还想到了其他的办法呢?比如扩展自己的UITabelViewCell子类?

 



原创粉丝点击