首页 文章

UICollectionView reloadData无法正常工作

提问于
浏览
1

在获取所使用的数据之后,它在 viewDidLoad 中被调用 . 在一些打印调试之后,如果没有更改数据,它看起来像调用所有适当的delegeate方法 . 如果某些数据已更改,则不会调用 cellForItemAt .

重新加载整个部分工作正常 . 但是给了我一个不需要的动画 . 尝试禁用之前的 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
}

Console Print/Log

Starting the app, going to the CollectionView tab where there are no favorites:

Load Favorites
Get number of section, 0
Get number of section, 0
Reload CollectionView
Get number of section, 0

Switching tab, and adding and setting an Item object's favorite to true, then heading back to the CollectionView tab:

Load Favorites
Favorites count: 1
Reload CollectionView
Get number of section, 1

The datamodel has 1 item, reloading CollectonView, cellForRowAtIndex not called?

Selecting another tab, random which, then heading back to the CollectionView tab, without changing any data.

Load Favorites
Favorites count: 1
Reload CollectionView
Get number of section, 1
Get cell

现在我的项目显示在列表中 . 您可以从Get Cell中看到 cellForItemAt 被称为aswell . 在最后两次记录之间没有任何变化,只需在标签上单击一次后退/第四次 .

忘了提,但这段代码在模拟器中运行良好 . 在读取设备上它只是没有给我任何错误,只是没有显示单元格(或调用 cellForItemAt

3 回答

  • 3

    经过一些调试后,当项目减少时出现错误(而不是像我上面尝试的那样增加) .

    UICollectionView received layout attributes for a cell with an index path that does not exist
    

    这导致了iOS 10 bug: UICollectionView received layout attributes for a cell with an index path that does not exist

    collectionView!.reloadData()
    collectionView!.collectionViewLayout.invalidateLayout()
    collectionView!.layoutSubviews()
    

    This solved my problem.

    我正在使用自动调整单元格,但我想这并不能解释为什么 cellForItemAt 没有被调用 .

  • 0

    发布您的代码 .

    一种可能性是你正在使用`URLSession并尝试告诉你的集合视图从它的委托方法/完成闭包更新,没有意识到该代码是在后台线程上运行而你不能从后台线程进行UI调用 .

  • 0

    我面临同样的问题,自我调整单元格,上面的解决方案也可以工作,但集合视图滚动位置重置并转到顶部 . 以下解决方案有助于 retain your scroll position .

    collectionView.reloadData()
    let context = collectionView.collectionViewLayout.invalidationContext(forBoundsChange: collectionView.bounds)
    context.contentOffsetAdjustment = CGPoint.zero
    collectionView.collectionViewLayout.invalidateLayout(with: context)
    collectionView.layoutSubviews()
    

相关问题