Ios UICollectionView不重新加载数据';我不能在swift 4中工作

Ios UICollectionView不重新加载数据';我不能在swift 4中工作,ios,swift,uicollectionview,Ios,Swift,Uicollectionview,我将ios应用程序代码迁移到Swift 4,在我的集合视图中重新加载数据的方法不起作用。我搜索并尝试了很多东西,但没有人成功。它在Swift 3中完美地工作了一个多月,所以我不知道发生了什么 此代码适用于以下情况之一: private func setLevelLessons(){ let operationsDB = DBOperations() self.indexInformation = operationsDB.getIndex(forLevel: se

我将ios应用程序代码迁移到Swift 4,在我的集合视图中重新加载数据的方法不起作用。我搜索并尝试了很多东西,但没有人成功。它在Swift 3中完美地工作了一个多月,所以我不知道发生了什么

此代码适用于以下情况之一:

private func setLevelLessons(){
        let operationsDB = DBOperations()
        self.indexInformation = operationsDB.getIndex(forLevel: self.currentLevel)
        self.numLessons = (self.indexInformation?.count)!
        DispatchQueue.main.async {
            self.lessonsCollectionView.reloadData()
        }
    }
我在单击按钮时调用此函数。单击时,我从数据库获取数据并重新加载集合视图。我检查了数据是否正确,所以这不是问题所在

我在调用下一个方法时打印项目,并在第一次和第二次单击时被调用(在按钮中不需要第二次,无论在屏幕中的何处)

可能发生什么情况?

问题已解决


我在更新到Swift4和Xcode 9.0时遇到了这个问题,但今天我将Xcode升级到9.0.1版本(昨天发布),并且
collectionView.reloadData()
再次工作。所以,如果您有这个问题,请确保您有最新的Xcode版本,或者等到下一个版本发布。

这是swift4(Xcode 9)中的临时解决方案

DispatchQueue.main.async{
让numberOfItems=collectionView.numberOfItems(第0节)

让indexPaths=[Int](0..检查
numorrowatindexpath
returncount@SalmanGhumsani该值是正确的,我从
numLessons
变量中获取。我遇到了类似的效果,并且在[collectionView重载数据]之前使用了[collectionView.collectionViewLayout invalidateLayout]。在我的例子中,collectionview viewcontroller(obj-c)是swift viewcontroller的子viewcontroller。@Apoc对我不起作用。谢谢你的回答。在某些情况下,它使用的是
collectionview.reloadSections([i])
,但这不是一个好的解决方案。
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
DispatchQueue.main.async {
  let numberOfItems = collectionView.numberOfItems(inSection: 0)
  let indexPaths = [Int](0..<numberOfItems).map{ IndexPath(row: $0, section: 0) }
  collectionView.reloadItems(at: indexPaths)
}