ios uitableview uitableviewcell重用旧数据 [英] ios uitableview uitableviewcell reuse has old data

查看:162
本文介绍了ios uitableview uitableviewcell重用旧数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们都知道可以恢复uitableview中的单元格,但我看到了一些奇怪的结果。
我创建一个自定义单元格,每个单元格有4列,每列都有一个图像
所以格式就像这样
| img | img | img | img |单元格返回后
,我正在配置每个单元格,一个示例是将第一列配置为图片1,然后将第二列配置为不同的图片



现在只需假设我在tableview中一次只能显示两行:

  | a0 | a1 | a2 | a3 | 
| b4 | b5 | b6 | b7 |
------单元格c对用户不可见------
| c8 | c9 | c10 | c11 |

当用户向下滚动时,上面的列'a'(a0-a3)将变为不可见,并且到目前为止,我知道单元格变得不可见将被重用于列c



上面的例子工作正常,如果我可以完全占据每个单元格/行,即我总共12图片。



但是如果我说了10张图片(最后一行只会填充2张,其他两张是空的,如下所示:

  | a0 | a1 | a2 | a3 | 
| b4 | b5 | b6 | b7 |
---- --row / cell'c'对用户不可见------
| c8 | c9 |空|空|

然而,问题出现了,当我向上滚动查看单元格/行'c'时,
i有以下图片:

  | a0 | a1 | a2 | a3 | 
------行/单元格'a'对用户不可见------
| b4 | b5 | b6 | b7 |
| c8 | c9 | a2 | a3 |

请注意,行/单元格c的最后两列应为空,但显示a2和a3而不是



我该如何解决这个问题?我正在考虑一种手动方式,即通过检查我是否应该在重用时删除不必要的视图等,但可能更复杂
是否有更系统的方法来做到这一点更简单,更清洁?



有没有办法我们可以要求退回的单元格忘记其内容,或者我必须自己做这个吗?



<谢谢

解决方案

我最近遇到了一个类似的问题。在子视图后覆盖prepareForReuse tableview单元修复它。得到了这里的答案


we all know that the cell in uitableview can be resued, but i am seeing some weird results. i am create a custom cell, each cell has 4 columns, each column has an image so format is like this |img|img|img|img| after the cell is returned, i am configuring each cell, an example would be configuring first column to picture 1, then second column to different picture

now just assuming that i can only display two rows at a time in the tableview:

|a0|a1|a2 |a3 |
|b4|b5|b6 |b7 |
------cell c not visible to user------
|c8|c9|c10|c11|

as user scrolls down, column 'a' (a0-a3) above, will become invisible, and as far i know that cell becomes invisible will be reused for column c

the example above works fine, if i can fully occupy every cell/row, i.e. i have total 12 pictures.

but if i have say 10 pictures (last row will be filled with only 2, and the other two are empty, like as the following:

|a0|a1|a2 |a3 |
|b4|b5|b6 |b7 |
------row/cell 'c' not visible to user------
|c8|c9|empty|empty|

however, problem arises, when i scroll up to see cell/row 'c' i have the following picture:

|a0|a1|a2 |a3 |
------row/cell 'a' not visible to user------
|b4|b5|b6 |b7 |
|c8|c9|a2|a3|

notice that the row/cell c's last two columns should be empty, but is showing a2 and a3 instead

how can i fix this? i am thinking about a manual way of doing this, i.e. by checking if i should remove unnecessary views etc when reusing, but could be more complicated is there a more systematic way of doing this that is easier and cleaner?

is there a way we could ask the returned cell to forget its content, or do i have to do this myself?

thanks

解决方案

I had a similiar issue recently. Override prepareForReuse after subclass the tableview cell fixed it. Got the answer from here

这篇关于ios uitableview uitableviewcell重用旧数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆