UITableView要求在屏幕上看不到的单元格

时间:2013-11-01 23:23:39

标签: ios uitableview uikit

我有一个UITableView,我填写了来自Web服务的数据。我使用循环缓冲区来缓存新数据并删除旧数据,因此内存不会过载。 当表视图调用cellForRowAtIndexPath并且视图控制器查询缓冲区以获取数据时,所有魔法都会发生。此外,正确的实现依赖于这样一个事实:只有当一个单元格在屏幕上可见时,表视图才会调用该方法。如果数据可用,我将它返回给视图控制器,如果没有,我返回nil,哪个视图控制器知道如何处理,并开始获取所需的数据。提取完成后,我在表上调用'reloadData',然后显示数据。

我有一个奇怪的问题,无处不在,即使用户滚动到第200行,表视图也会向视图控制器询问索引路径为零或一行的行。在那一点上,显示的内容是错误的,因为缓冲区实现和尺寸。

使用调试器输出来说明来自cellForRowAtIndexPath:

 po indexPath <NSIndexPath: 0xb02b100> {length = 2, path = 0 - 0}

 po [self.tableView indexPathsForVisibleRows] <__NSArrayM 0x9b8f7f0>( <NSIndexPath: 0x9b42910> {length = 2, path = 0 - 19}, <NSIndexPath: 0x9b82080> {length = 2, path = 0 - 20}, <NSIndexPath: 0x9b6e2a0> {length = 2, path = 0 - 21}, <NSIndexPath: 0x9b39010> {length = 2, path = 0 - 22}, <NSIndexPath: 0x9b42d80> {length = 2, path = 0 - 23}, <NSIndexPath: 0x9b98a20> {length = 2, path = 0 - 24}, <NSIndexPath: 0x9b9dc50> {length = 2, path = 0 - 25}, <NSIndexPath: 0x9b9e4d0> {length = 2, path = 0 - 26}, <NSIndexPath: 0x9b40740> {length = 2, path = 0 - 27}, <NSIndexPath: 0x9b40750> {length = 2, path = 0 - 28}, <NSIndexPath: 0x9b88150> {length = 2, path = 0 - 29}, <NSIndexPath: 0x9b88160> {length = 2, path = 0 - 30}, <NSIndexPath: 0x9b97120> {length = 2, path = 0 - 31}, <NSIndexPath: 0x9b97130> {length = 2, path = 0 - 32}, <NSIndexPath: 0x9b44cf0> {length = 2, path = 0 - 33}, <NSIndexPath: 0x9b44d00> {length = 2, path = 0 - 34}, <NSIndexPath: 0x9b8d580> {length = 2, path = 0 - 35}, <NSIndexPath: 0x9b8d590> {length = 2, path = 0 - 36} )

有没有人遇到过这样奇怪的问题?

1 个答案:

答案 0 :(得分:0)

表视图的cellForRowAtIndexPath:不会被调用以仅获取需要可见的单元格;表视图通常会抓取几个额外的单元格,以便在用户滚动时准备就绪。

要说服自己,请记录cellForRowAtIndexPath:的返回值。如果6个单元格可见,我打赌cellForRowAtIndexPath:被调用超过6次。