UICollectionView重载数据在swift中不起作用4
我将我的iOS应用程序代码迁移到Swift 4,并且在我的集合视图中重新载入数据方法不起作用。我搜索并尝试了很多东西,但没有人工作。它在Swift 3中工作了一个多月,所以我不知道发生了什么。UICollectionView重载数据在swift中不起作用4
此代码是案例之一:
private func setLevelLessons(){
let operationsDB = DBOperations()
self.indexInformation = operationsDB.getIndex(forLevel: self.currentLevel)
self.numLessons = (self.indexInformation?.count)!
DispatchQueue.main.async {
self.lessonsCollectionView.reloadData()
}
}
我调用此函数一个按钮被点击时。点击时,我从数据库中获取数据并重新加载收藏视图。我检查数据是否正确,这不是问题。
我打印的项目时,下一个方法被调用,并在第一和第二次点击被称为(在按钮第二没有必要,无论在屏幕)
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
可能是什么回事?
问题解决了。
我在更新到Swift4和Xcode 9.0时遇到了这个问题,但今天我将Xcode升级到9.0.1版本(昨天发布),并且collectionView.reloadData()
正在重新运行。所以,如果你有这个问题,确保你有最新的Xcode版本,或者等到下一个版本发布。
它仍然在Xcode 9.2,swift 4 –
这是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)
}
不工作Xcode 9.2,swift 4 –
检查'numOfRowAtIndexPath'返回计 –
@SalmanGhumsani该值是正确的,我把它从'numLessons'变量。 – CipherCit
我遇到过类似的效果,对于我_ _ collectionView reloadData _ _之前的_ [collectionView.collectionViewLayout invalidateLayout] _工作。在我的情况下,collectionview-viewcontroller(obj-c)是一个快速视图控制器的子视图控制器。 – Apoc