UITableView的indexPathsForVisibleRows不正确?

时间:2010-11-04 17:02:21

标签: iphone uitableview

当我在indexPathsForVisibleRows期间检查表格的可见indexPaths时带有“UIScrollViewDelegate's scrollViewDidScroll and scrollViewDidEndDragging”时,它似乎是准确的。

但有时只是在滚动和拖动结束并且调用“cellForRowAtIndexPath”时,对“indexPathsForVisibleRows”的调用返回0.屏幕上有可见的行,所以我知道它不能一旦发生这种情况,我可以调用UITableView::visibleCells并获得非零值。

为什么存在这种差异?

2 个答案:

答案 0 :(得分:13)

您是否尝试按以下顺序调用方法:

[MyTable visibleCells];
[MyTable indexPathsForVisibleRows];

iOS中存在一个带有indexPathsForVisibleRows的错误。使用上面两行代码来获得正确的可见行。

答案 1 :(得分:0)

快速运行,

let viscells = tableView.visibleCells
let isThisCellVisible = (tableView.indexPathsForVisibleRows ?? []).contains(indexPath)

但这不是

_ = tableView.visibleCells
let isThisCellVisible = (tableView.indexPathsForVisibleRows ?? []).contains(indexPath)

visibleCells和NSFetchedResultsController

注意访问visibleCells的方式。如果您[attempt] to access the table view's visibleCells while they were in the process of being updated, which is not allowed.

,则用户界面将无限期冻结

例如tableView.beginUpdates() / tableView.endUpdates()controllerWillChangeContent(_:) / controllerDidChangeContent(_:)等的使用方式

如果发生这种情况,完整的调试消息是:

[Assert] Attempted to access the table view's visibleCells while they were in the process of being updated, which is not allowed. Make a symbolic breakpoint at UITableViewAlertForVisibleCellsAccessDuringUpdate to catch this in the debugger and see what caused this to occur. Perhaps you are trying to ask the table view for the visible cells from inside a table view callback about a specific row?