解决关于cell重用机制导致数据显示错乱的三种办法

  1. 方法1 将获得cell的方法从- (UITableViewCell*)dequeueReusableCellWithIdentifier:(NSString*)identifier 换为-(UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath

  2.  

  3. 重用机制调用的就是dequeueReusableCellWithIdentifier这个方法,方法的意思就是“出列可重用的cell”,因而只要将它换为cellForRowAtIndexPath(只从要更新的cell的那一行取出cell),就可以不使用重用机制,因而问题就可以得到解决,虽然可能会浪费一些空间。

  4.  

  5. 示例代码:

  6.  

  7.  

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

  9. {

  10.    static NSString *CellIdentifier = @"Cell";

  11.    // UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //改为以下的方法

  12.    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; //根据indexPath准确地取出一行,而不是从cell重用队列中取出

  13.    if (cell == nil) {

  14.        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

  15.    }

  16.     //...其他代码                              

  17. }

  18.  

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

  20. {

  21.    static NSString *CellIdentifier = @"Cell";

  22.    // UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //改为以下的方法

  23.    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; //根据indexPath准确地取出一行,而不是从cell重用队列中取出

  24.    if (cell == nil) {

  25.        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

  26.    }

  27.     //...其他代码                              

  28. }

  29.  

  30.  

  31.  

  32.  

  33.  

  34.  

  35.  

  36. 方法2 通过为每个cell指定不同的重用标识符(reuseIdentifier)来解决。

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

  38.  

     

  39. 般用这个方法比较好!

  40.  

  41. 示例代码:

  42.  

  43.  

  44. [plain]

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

  46. {

  47.  

  48.    NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%d%d", [indexPath section], [indexPath row]];//以indexPath来唯一确定cell

  49.    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //出列可重用的cell

  50.    if (cell == nil) {

  51.        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

  52.    }

  53.    //...其他代码

  54. }

  55.  

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

  57. {

  58.  

  59.    NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%d%d", [indexPath section], [indexPath row]];//以indexPath来唯一确定cell

  60.    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //出列可重用的cell

  61.    if (cell == nil) {

  62.        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

  63.    }

  64.    //...其他代码

  65. }

  66.  

  67. 方法3 删除重用cell的所有子视图

  68.  

  69. 这个方法是通过删除重用的cell的所有子视图,从而得到一个没有特殊格式的cell,供其他cell重用。

  70.  

  71. 这个方法有一个问题,就是如果cell中有图片的话,会导致之前的cell的图片也会没有了。

  72. 示例代码:

  73.  

  74.  

  75. [plain]

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

  77. {

  78.    static NSString *CellIdentifier = @"Cell";

  79.    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //出列可重用的cell

  80.    if (cell == nil) {

  81.        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

  82.    }

  83.    else

  84.    {

  85.        //删除cell的所有子视图

  86.        while ([cell.contentView.subviews lastObject] != nil)

  87.        {

  88.            [(UIView*)[cell.contentView.subviews lastObject] removeFromSuperview];

  89.        }

  90.    }

  91.    //...其他代码

  92. }



posted on 2016-03-18 15:41  Mr_Deng  阅读(886)  评论(0编辑  收藏  举报

导航