(转)UITableViewCell复用问题

列举两个场景对比一下,也许tableviewcell的复用就很清晰明了了。

1,
 1 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
 2 
 3      static  NSString *CellIdentifier =  @"cell1";
 4      UITableViewCell *cell  =[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
 5 
 6     if (cell == nil) {
 7 
 8        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
 9 
10         UILabel *labelTest = [[UILabel alloc]init];
11 
12         [labelTest setFrame:CGRectMake(2, 2, 80, 40)];
13 
14         [labelTest setBackgroundColor:[UIColor clearColor]];
15 
16         [labelTest setTag:1];
17 
18         [[cell contentView]addSubview:labelTest];
19 
20     }
21 
22     UILabel *label1 = (UILabel*)[cell viewWithTag:1];
23 
24     [label1 setText:[self.tests objectAtIndex:indexPath.row]];
25 
26     return cell;
27 
28 }        
2,
 1 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
 2 
 3      static  NSString *CellIdentifier =  @"cell1";
 4     UITableViewCell *cell  =[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
 5 
 6     if (cell == nil) {
 7 
 8         cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
 9 
10  }
11 
12     UILabel *labelTest = [[UILabel alloc]init];
13 
14     [labelTest setFrame:CGRectMake(2, 2, 80, 40)];
15 
16     [labelTest setBackgroundColor:[UIColor clearColor]];  //之所以这里背景设为透明,就是为了后面让大家看到cell上叠加的label。
17 
18     [labelTest setTag:1];
19 
20     [[cell contentView]addSubview:labelTest];
21 
22     [labelTest setText:[self.tests objectAtIndex:indexPath.row]];
23 
24 return cell;
25 }
当你上下来回滑动tableview的时候就会看到区别,第一种程序界面不会出现异常,但是第二种就不是了,会出现字体叠加现象,其实更确切的是多个label的叠加。为什么呢,因为在tableview刷新的时候,如果那个位置已经有现成的cell,它就不会再重新请求资源生成新的cell了,而是复用原来的cell。所以对于对于第一种,代码的思路是第一次在cell不存在的时候生成cell,定义cell样式,以后不管是刷新还是重新请求还好,它都只是复用已生成的cell。而第二种思路是,在cell不存在的时候,请求生成cell,然后给cell上添加label,刷新的时候,会复用已有的cell,但是会重复添加label,故造成重叠的现象。
 
之前类似的问题来回困扰了我好多次,我都没有下决心彻底搞清楚,每次都是得过且过,只要程序最好调好了,就OK。今天又碰到了类似的问题,终于大致搞清楚了,希望以后不会再被它坑害。
 
 
posted @ 2014-03-07 23:58  fan_yufan  阅读(321)  评论(0编辑  收藏  举报