iOS - UITableView中Cell重用机制导致Cell内容出错的解决办法

  "UITableView" iOS开发中重量级的控件之一;在日常开发中我们大多数会选择自定Cell来满足自己开发中的需求, 但是有些时候Cell也是可以不自定义的(比如某一个简单的页面,只需要展示一些简单的信息);但是当页面大于屏幕显示的范围的时候, 滑动UITableView的时候,Cell上的内容会出现混乱或者错误的现象,经过反复的查找问题应该是出现在UITableViewCell的重用机制上;那么下面我们就来说一下解决这种问题的几种办法,以及最好的解决办法:

(一)使用系统的Cell,简单的Cell重用机制我们会这样去写:

 1 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
 2     static NSString *cellIdentifier = @"cellID";
 3     
 4     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
 5     
 6     if (!cell) {
 7         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
 8     }
 9     
10     // 说明: 要测试Cell内容是否错乱, 要将每行Cell显示的内容不同, 才能看出来
11     cell.textLabel.text = @"Cell显示内容";
12     cell.detailTextLabel.text = @"Cell辅助显示内容";
13     
14     return cell;
15 }

  这样写虽然达到了Cell的重用, 但是会出现我们要讨论的话题(滑动UITableView出现Cell内容混乱的情况),那么下面我们就来说一下避免Cell内容出现混乱的写法;

 

(二)使用系统的Cell,避免Cell内容出现混乱的写法:

  (1)方法一:

    ①将获得Cell的方法:- (UITableViewCell *)dequeueReusableCellWithIdentifier:(NSString *)identifier; 换为:- (UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath;

    ②解决了Cell内容混乱的现象,但是没有能够达到Cell的重用机制的原理;因为cellForRowAtIndexPath(只从要更新的cell的那一行取出cell),不使用重用机制,因而问题就可以得到解决,但是会浪费一些空间(真机测试比其他方法略显卡顿);

    ③示例代码:

 1 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
 2     static NSString *cellIdentifier = @"cellID";
 3     
 4     // UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
 5     // 将上面的方法换成(根据indexPath准确地取出一行, 而不是从cell重用队列中取出, 方法如下:)
 6     UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
 7     
 8     if (!cell) {
 9         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
10     }
11     
12     // 说明: 要测试Cell内容是否错乱, 要将每行Cell显示的内容不同, 才能看出来
13     cell.textLabel.text = @"Cell显示内容";
14     cell.detailTextLabel.text = @"Cell辅助显示内容";
15     
16     return cell;
17 }

  (2)方法二:

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

    ②重用机制是根据相同的标识符来重用Cell的,标识符不同的Cell不能彼此重用;我们将每个Cell的标识符都设置为不同,就可以避免不同Cell重用的问题了;

    ③示例代码:

 1 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
 2     // static NSString *cellIdentifier = @"cellID";
 3     // 将上面的方法换成(以indexPath来唯一确定Cell, 方法如下:)
 4     NSString *cellIdentifier = [NSString stringWithFormat:@"Cell%zd%zd", [indexPath section], [indexPath row]];
 5     
 6     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
 7     
 8     if (!cell) {
 9         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
10     }
11     
12     // 说明: 要测试Cell内容是否错乱, 要将每行Cell显示的内容不同, 才能看出来
13     cell.textLabel.text = @"Cell显示内容";
14     cell.detailTextLabel.text = @"Cell辅助显示内容";
15     
16     return cell;
17 }

  (3)方法三:

    ①通过删除重用的Cell的所有子视图,从而得到一个没有特殊格式的Cell,供其他Cell重用;

    ②示例代码:

 1 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
 2     static NSString *cellIdentifier = @"cellID";
 3     
 4     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
 5     
 6     if (!cell) {
 7         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
 8     } else {
 9         // 删除Cell的所有子视图
10         while ([cell.contentView.subviews lastObject] != nil) {
11             [(UIView*)[cell.contentView.subviews lastObject] removeFromSuperview];
12         }
13     }
14     
15     // 说明: 要测试Cell内容是否错乱, 要将每行Cell显示的内容不同, 才能看出来
16     cell.textLabel.text = @"Cell显示内容";
17     cell.detailTextLabel.text = @"Cell辅助显示内容";
18     
19     return cell;
20 }

  (以上便是对相关知识的相关介绍和理解,还希望大家相互补充共同进步)

posted @ 2016-05-05 15:11  DDChina  阅读(3592)  评论(0编辑  收藏  举报