在获取使用的数据后,它在viewDidLoad
中被调用。 经过一些打印调试后,如果没有数据更改,它看起来像调用所有适当的删除方法。如果更改了某些数据,则不会调用cellForItemAt
。UICollectioView reloadData不起作用
重新加载整个部分工作正常。但给我一个不需要的动画。试过以前禁用UIView
动画,并且重新启动后启用节,但仍给我一点动画。
collectionView.reloadSections(IndexSet(integer: 0))
这里是我目前的情况,使用reloadData时()
的UICollectionViewController是TabBarController的一部分。 我使用自定义的UICollectionViewCells。数据从CoreData加载。
第一次打开标签,其工作正常。 更新另一个选项卡中的收藏夹项目并返回到此collectionView后,其未更新。但是,如果我选择另一个选项卡,并返回到这一个,它的更新。
var favorites = [Item]()
override func viewWillAppear(_ animated: Bool) {
collectionView!.register(UINib.init(nibName: reuseIdentifier, bundle: nil), forCellWithReuseIdentifier: reuseIdentifier)
if let flowLayout = collectionView!.collectionViewLayout as? UICollectionViewFlowLayout {
flowLayout.estimatedItemSize = CGSize(width: 1,height: 1)
}
loadFavorites()
}
func loadFavorites() {
do {
print("Load Favorites")
let fetch: NSFetchRequest = Item.fetchRequest()
let predicate = NSPredicate(format: "favorite == %@", NSNumber(value: true))
fetch.predicate = predicate
favorites = try managedObjectContext.fetch(fetch)
if favorites.count > 0 {
print("Favorites count: \(favorites.count)")
notification?.removeFromSuperview()
} else {
showEmptyFavoritesNotification()
}
print("Reload CollectionView")
collectionView!.reloadData(
} catch {
print("Fetching Sections from Core Data failed")
}
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
print("Get number of section, \(favorites.count)")
if favorites.count > 0 {
return favorites.count
} else {
return 0
}
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
print("Get cell")
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! SubMenuCell
let menuItem = favorites[indexPath.row]
cell.title = menuItem.title
cell.subtitle = menuItem.subtitle
return cell
}
控制台打印/登录
启动应用程序,去的CollectionView标签那里有没有收藏:
Load Favorites
Get number of section, 0
Get number of section, 0
Reload CollectionView
Get number of section, 0
开关选项卡,并添加和设置一个项目对象的最爱为true,然后返回到CollectionView选项卡:
Load Favorites
Favorites count: 1
Reload CollectionView
Get number of section, 1
The datamodel has 1 item, reloading CollectonView, cellForRowAtIndex not called?
选择另一个标签,随机选择其中的标签,然后返回到CollectionView选项卡,而不更改任何数据。
Load Favorites
Favorites count: 1
Reload CollectionView
Get number of section, 1
Get cell
现在我的物品显示在列表中。您可以从Get Cell中看到cellForItemAt
也被称为。这些最后两次登录之间没有任何变化,只是一次在选项卡上单击/第四次。
忘了提及,但这段代码在模拟器中工作正常。 在读取设备上它只是没有给我任何错误,只是没有显示单元格(或拨打cellForItemAt
)
你可以更具体一些什么不工作,如果你有一切正确的设置,没有办法调用''collectionView'上的reloadData()'不会调用'cellForItemAtIndexPath'每个可见的项目(行)... – Ladislav