[英]UICollectionView reload data doesn't work in swift 4
I migrated my ios app code to Swift 4 and reload data method in my collection views doesn't work. 我将我的ios应用程序代码迁移到Swift 4并在我的集合视图中重新加载数据方法不起作用。 I searched and try a lot of things but no one worked.
我搜索并尝试了很多东西,但没有人工作。 It was working perfectly for more than a month in Swift 3, so I don't know what is happening.
它在Swift 3中运行了一个多月,所以我不知道发生了什么。
This code is for one of the cases: 此代码适用于以下情况之一:
private func setLevelLessons(){
let operationsDB = DBOperations()
self.indexInformation = operationsDB.getIndex(forLevel: self.currentLevel)
self.numLessons = (self.indexInformation?.count)!
DispatchQueue.main.async {
self.lessonsCollectionView.reloadData()
}
}
I call this function when a button is clicked. 单击按钮时调用此函数。 When is clicked I get data from database and reload collection view.
单击时,我从数据库获取数据并重新加载集合视图。 I checked if data is correct so that's not the problem.
我检查了数据是否正确,这不是问题所在。
I printed items when next method is called, and is called in the first and second click (second not necessary in the button, no matter where in the screen) 我在调用下一个方法时打印了项目,并在第一次和第二次单击时调用(第二次不需要按钮,无论在屏幕的哪个位置)
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
What could be happening? 可能会发生什么?
Problem solved. 问题解决了。
I had this problem when update to Swift4 and Xcode 9.0, but today I upgrade Xcode to 9.0.1 version (released yesterday) and collectionView.reloadData()
is working again. 我更新到Swift4和Xcode 9.0时遇到了这个问题,但今天我将Xcode升级到9.0.1版本(昨天发布),并且
collectionView.reloadData()
再次运行。 So, if you have this problem ensure you have last Xcode version or wait until next version is released. 因此,如果您遇到此问题,请确保您拥有最新的Xcode版本或等到下一个版本发布。
It is a temporary solution in swift4 (Xcode 9). 它是swift4(Xcode 9)中的临时解决方案。
DispatchQueue.main.async {
let numberOfItems = collectionView.numberOfItems(inSection: 0)
let indexPaths = [Int](0..<numberOfItems).map{ IndexPath(row: $0, section: 0) }
collectionView.reloadItems(at: indexPaths)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.