iOS中TableViewController的cell重用出错

作者: 神经骚栋 | 来源:发表于2016-01-08 15:59 被阅读9115次

用我的双手,成就你的梦想 ---栋哥

今天我的心爱的程序又出现bug,对于我这个小菜来说,不得不说是非常苦恼的,主要是cell加载的时候因为重用池的问题而出现各种的bug,虽然程序没有崩掉,但是大大影响到我的心情,下面是最主要的一个问题 ,就是cell的重用问题, cell因为从重用池中调取,没有及时删除上面的内容而导致内容的各种出现, 这里有几个解决方案.

UITableView继承自UIScrollview,是苹果为我们封装好的一个基于scroll的控件。上面主要是一个个的UITableViewCell,可以让UITableViewCell响应一些点击事件,也可以在UITableViewCell中加入UITextField或者UITextView等子视图,使得可以在cell上进行文字编辑。

UITableView中的cell可以有很多,一般会通过重用cell来达到节省内存的目的:通过为每个cell指定一个重用标识符(reuseIdentifier),即指定了单元格的种类,当cell滚出屏幕时,会将滚出屏幕的单元格放入重用的queue中,当某个未在屏幕上的单元格要显示的时候,就从这个queue中取出单元格进行重用。

但对于多变的自定义cell,有时这种重用机制会出错。比如,当一个cell含有一个UITextField的子类并被放在重用queue中以待重用,这时如果一个未包含任何子视图的cell要显示在屏幕上,就会取出并使用这个重用的cell显示在无任何子视图的cell中,这时候就会出错。

代码如下

三种方法各有利端,

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

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

示例代码:


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    // UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //改为以下的方法 
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; //根据indexPath准确地取出一行,而不是从cell重用队列中取出 
    if (cell == nil) { 
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 
     //...其他代码                               
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    // UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //改为以下的方法
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; //根据indexPath准确地取出一行,而不是从cell重用队列中取出
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
     //...其他代码                              
}

 

 

 

方法2 通过为每个cell指定不同的重用标识符(reuseIdentifier)来解决。
重用机制是根据相同的标识符来重用cell的,标识符不同的cell不能彼此重用。于是我们将每个cell的标识符都设置为不同,就可以避免不同cell重用的问题了。

示例代码:


[plain]
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
     
    NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%d%d", [indexPath section], [indexPath row]];//以indexPath来唯一确定cell 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //出列可重用的cell 
    if (cell == nil) { 
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 
    //...其他代码 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   
    NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%d%d", [indexPath section], [indexPath row]];//以indexPath来唯一确定cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //出列可重用的cell
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    //...其他代码
}

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

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




示例代码:


[plain]
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; //出列可重用的cell 
    if (cell == nil) { 
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 
    else 
    { 
        //删除cell的所有子视图 
        while ([cell.contentView.subviews lastObject] != nil) 
        { 
            [(UIView*)[cell.contentView.subviews lastObject] removeFromSuperview]; 
        } 
    } 
    //...其他代码 
} 

相关文章

网友评论

  • bd4fa1179a8a:好棒棒哟~:+1:
  • timeforasong:第三种方法你们用的时候有没有出现略微卡顿?怎么解决啊?
  • 水寒不知:第三种方式删除了所有的子视图。tableview不还是空的吗
    神经骚栋:@水寒不知 在删除之后有你的其他操作呀,先删除再添加:smile::smile::smile:
  • feng_dev:故事板方式实现的 怎么处理
    feng_dev:@神经骚栋 原来是我不小心在cell里面修改了数据源
    神经骚栋:@伤感的小孩 故事版的不知道,xib可以看我的另外一篇博客http://www.jianshu.com/p/9c7a8a57515c
  • 我是码神:我也用的第三种方法 全都是空的
  • a8dce9b5a213:用了第三种方法,但是cell都是空的,是怎么回事?@神经骚栋
    a8dce9b5a213:@神经骚栋 删除了,但是cell的内容还是空白...... :fearful:
    神经骚栋:@Justin雨辰 因为这个方法就是注册的实现~~~是以前的方法,这是现在用的少,
    神经骚栋:@Justin雨辰 你注册了cell了吧? 你把注册cell那行代码删除就好~
  • 超_iOS:此文感觉见得太多,也不知道到底是谁原创了..
    神经骚栋: @李二超 没有,转载就是转载,没什么说不得的,第三种的确是比较好用一点,如果使用的xib方式,可以看一下我上面的一篇关于cell重用出错的博客,希望能对你有所帮助
    超_iOS:@神经骚栋 额,只是吐个槽.大神,第三种是不是比较好点?
    神经骚栋: @李二超 嗯,这篇非原创……见谅
  • Jerry在种草:楼主为什么不考虑prepareForReuse呢,在重用时初始化一遍。
    我感觉是你对uitableview的理解不对,因为它本身需要的就是一些高度相似的cell。
    chenyu1520:没错,其实使用 prepareForReuse 将 cell 上的数据清楚即可。如果项目中需要空白的 cell,这就需要另一种 identifier 的 cell。一般出现数据错乱,是 cell 上有图片,如果你没有清楚,滑动较快时,新的图片又没有加载出来,会出现一些问题。
    我是码神:@Jerry在种草 prepareForReuse 怎么用
    马铃薯蜀黍:@Jerry在种草 求指教 最近遇到这个问题很着急

本文标题:iOS中TableViewController的cell重用出错

本文链接:https://www.haomeiwen.com/subject/nqofkttx.html