1

我有一个 UITableView 填充来自网络服务的数据。我使用循环缓冲区来缓存新数据并删除旧数据,因此内存不会过载。当表视图调用 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} )

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

4

1 回答 1

0

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

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

于 2013-11-01T23:51:29.133 回答