Ios UITableView调用didEndDisplayingCell不正确

Ios UITableView调用didEndDisplayingCell不正确,ios,swift,uitableview,swift3,Ios,Swift,Uitableview,Swift3,我在使用UITableView的didendisplayingcell方法时遇到问题。我正在使用此方法与willDisplayCell一起处理表的一些额外数据跟踪。问题是,didendisplayingcell在一行中被调用了好几次,而且在单元格实际仍在显示的时候。我不明白为什么会发生这种情况 更多详细信息 我正在使用NSFetchedResultsController为表提供数据。更改从不同队列上的NSManagedObjectContext进入表中,然后合并到表使用的NSManagedObj

我在使用
UITableView
didendisplayingcell
方法时遇到问题。我正在使用此方法与willDisplayCell一起处理表的一些额外数据跟踪。问题是,
didendisplayingcell
在一行中被调用了好几次,而且在单元格实际仍在显示的时候。我不明白为什么会发生这种情况

更多详细信息

我正在使用NSFetchedResultsController为表提供数据。更改从不同队列上的
NSManagedObjectContext
进入表中,然后合并到表使用的
NSManagedObjectContext
。这一切似乎都运作良好。看看我对上述方法的实现:

override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    debugPrint("willDisplay cell at \(indexPath).")
    self.dataProvider.addTrackedRelatedRecords(for: indexPath)
}

override func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    debugPrint("didEndDisplaying cell at \(indexPath).")
    self.dataProvider.removeTrackedRelatedRecords(for: indexPath)
}
我在这里看到的输出是:

 "Updating rows in table at [0, 0]". <=== This is an update to the data
 "willDisplay cell at [0, 0]."       <=== This is what I expect
 "didEndDisplaying cell at [0, 0]"
 "didEndDisplaying cell at [0, 0]"
 "didEndDisplaying cell at [0, 0]"
这样可以正确地看到单元格仍然可见,因此不会删除我的额外跟踪。但是,如果单元格可见,为什么要调用该方法

显然,在我的应用程序中还有很多其他代码在起作用。我将尝试将其归纳为一个非常简单的案例,但我想先发布这篇文章,看看是否有人看到过可能发生这种情况的案例。那也许能帮我找到它


感谢您的帮助。

我发现了这个问题,我正在发布一个答案,以防其他人看到这个问题,我的发现会对他们有所帮助

原来我无意中在
tableView.beginUpdates()和
tableView.endUpdates()块中留下了一个
tableView.reloadRows(位于:[indepath],带:.fade)

我认为,
reloadRows
导致单元格被替换,这将导致
didendisplayingcell
。而且,由于块内发生了几次更新,因此在调用
tableView.endUpdates()
后,这些更新将被排队并发布


删除
reloadRows
调用修复了该问题,所有操作都按预期进行。

在我的情况下,这是因为每当我更改
centeredIndexPath
时,我都在调用
reloadData()
。确保您没有重新加载数据,因为所有单元格将调用
didendisplaying
(当您重新加载数据时)。

您能确定这是在特定版本的iOS中发生的吗?@holly,问得好。我将在iOS 9.3上尝试同样的方法,看看我得到了什么。@holly,我刚刚在iOS 9.3.5上尝试了这个方法,得到了同样的结果。最初的测试是在iOS 10.3.1上进行的。我有完全相同的问题,如果我删除“重新加载行”,那么我如何更新这些行?当NSFetchedResultsChangeType为.update时,我正在重新配置单元格(而不是重新加载),并且在第一个单元格(新添加的单元格)仍然可见时,我多次调用了DiEndDisplaying。换言之,我执行一次拉式刷新,它调用.insert和.update,新结果显示在IndexPath 0,0上,而DiEndDisplaying则为IndexPath 0,0调用。这意味着暂停下载手机应该显示的媒体内容。
override func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    debugPrint("didEndDisplaying cell at \(indexPath).")
    // WORK AROUND
    if let visibleRows = tableView.indexPathsForVisibleRows, visibleRows.contains(indexPath) {
        return
    }
    self.dataProvider.removeTrackedRelatedRecords(for: indexPath)
}