【问题标题】:Reloading data from a tableView within a collectionViewCell从 collectionViewCell 中的 tableView 重新加载数据
【发布时间】:2017-01-31 16:23:16
【问题描述】:

我在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

【问题讨论】:

  • 您还需要从数组中删除对象。
  • 我已经在实际代码中做到了,我只是简化了这里的结构。我这样做的方法是获取 selectedIndexPaths 并应用过滤器来删除 selectedIndexPaths 处的项目。

标签: ios uitableview swift3 uicollectionviewcell tableviewcell


【解决方案1】:

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

Dispatch.main.async {

    cleanItems(completion: { (success) in 


    etc.


    })

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-11
    • 1970-01-01
    • 1970-01-01
    • 2016-03-06
    相关资源
    最近更新 更多