2013-11-01 58 views
1

我有一個UITableView,它填充了來自Web服務的數據。我使用循環緩衝區來緩存新數據並刪除舊數據,因此內存不會超載。 當表視圖調用cellForRowAtIndexPath並且視圖控制器諮詢緩衝區以獲取數據時,所有的奇蹟都會發生。另外,正確的實現依賴於這樣一個事實,即當單元格在屏幕上可見時,表格視圖只會調用該方法。如果數據可用,則將其返回給視圖控制器,否則返回nil,哪個視圖控制器知道如何處理,並開始獲取所需的數據。提取完成後,我會在表上調用'reloadData',並顯示數據。UITableView要求在屏幕上不可見的單元格

我有一個奇怪的問題,即使用戶滾動到第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}) 

有沒有人遇到過這樣奇怪的問題?

回答

0

表視圖的cellForRowAtIndexPath:不被調用來只抓取需要可見的單元格;表格視圖通常會抓取一些額外的單元格,以便在用戶滾動時準備好。

要說服自己,請記錄cellForRowAtIndexPath的返回值:.如果有6個單元格可見,我敢打賭cellForRowAtIndexPath:被調用了6次以上。

+0

我不明白爲什麼會被稱爲超過6次? willShowCellAtIndexPath還會記錄顯示indexPath的第0行。 – foFox

+0

表視圖框架緩衝其表格單元格。至於爲什麼表格視圖在表格底部請求第0行時,我無法給出明確的答案。 – bilobatum

相關問題