从 collectionViewCell 中的 tableView 重新加载数据

Reloading data from a tableView within a collectionViewCell

我在 collectionViewCell 中有一个 tableView,当我尝试重新加载数据时出现错误。

*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3600.6.21/UITableView.m:1610

我尝试使用 Dispatch.main.async 似乎解决了这个问题。唯一的问题是它不会重新加载数据并且 tableView

中没有任何变化
func cleanItems(completion: @escaping (Bool) -> Void) {

    Dispatch.main.async {

        cell.tableView.beginUpdates()
        // Make changes in the Data Source
        cell.tableView.deleteRows(at: selectedItems, with: .fade)
        cell.tableView.endUpdates()

        // Reloading sections accordingly depending on what the user has deleted

        // Do I need to reload data here again? It used to work without Dispatch, but it wasn't stable
        cell.tableView.reloadData()


        // Updating items with if statements to reload data in Firebase

        completion(true)

    }
}

这根本不会重新加载数据,而且似乎没有任何变化。好消息是我没有随机崩溃,在实施 Dispatch.main.async

之前就是这种情况

我检索了每个部分中的 numberOfRows 以查看结束更新后有多少行。

print(cell.tableView.numberOfRows(inSection: 1))

我得到的行数与当前视图中的行数相同。

这很关键,因为如果 tableView 部分全部为零,则 collectionViewCell 应该消失。我们永远不会到达 completion 块,因为它说 numberOfRows 从未改变过。留给我们一个未更新的 tableView.

我通过将 Dispatch.main.async 移到函数调用之外解决了这个问题。

Dispatch.main.async {

    cleanItems(completion: { (success) in 


    etc.


    })

}