【问题标题】:iOS 9 UICollectionView horizontal reload data does not workiOS 9 UICollectionView 水平重新加载数据不起作用
【发布时间】:2016-01-02 16:41:42
【问题描述】:

注意:ios 开发新手。

我刚刚使用垂直滚动创建了一个集合视图,它在动态添加新单元格时有效,但是当我将滚动更改为 horizontal 时它不起作用。我设法通过在主线程上重新加载数据来修复它。

我的问题是:

  1. 为什么我在使用垂直滚动时不需要显式在主线程上重新加载数据?
  2. viewWillAppear 中重新加载数据/重新加载特定单元格或部分数据是最佳做法吗?

有人遇到过这个问题吗?使用 XCode 7 iPhone 6 和 iPhone 6s(全都是 iOS9)进行测试。

我重新加载数据的代码(用于测试目的)在viewWillAppear

override func viewWillAppear(animated: Bool) {
  super.viewWillAppear(animated)
  myViewModel.reload() // Reload the data with fresh data
  collectionView.reloadData()
}

在另一个场景中,我显示了一个 Input 和一个按钮,当按下该按钮时,它会将一条记录附加到内存中的数组中(静态)

@IBAction func create() {
  myViewModel.create(someInput.text!).persist() // Append to an array in memory (just for testing)
  navigationController?.popViewControllerAnimated(true)
}

【问题讨论】:

  • 我也有同样的问题,请问您找到解决方法了吗?

标签: ios swift uicollectionview


【解决方案1】:

reloadData() 必须在主线程中执行:

override func viewWillAppear(_ animated: Bool) {
    DispatchQueue.main.async(execute: {
        self.collectionView.reloadData()
    })
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多