2017-10-14 43 views
1

在获取使用的数据后,它在viewDidLoad中被调用。 经过一些打印调试后,如果没有数据更改,它看起来像调用所有适当的删除方法。如果更改了某些数据,则不会调用cellForItemAtUICollectioView 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

+0

你可以更具体一些什么不工作,如果你有一切正确的设置,没有办法调用''collectionView'上的reloadData()'不会调用'cellForItemAtIndexPath'每个可见的项目(行)... – Ladislav

回答

0

发布您的代码。

一种可能性是您正在使用URLSession并试图告诉您的集合视图从其委托方法/完成闭包更新,而不是意识到该代码在后台线程上运行,并且您无法执行UI调用从后台线程。

+0

我没有使用URLSession。还尝试在后台和主线程中运行reloadData()。忘了写它在模拟器中的工作,但不是在真实的设备上。用代码更新了我最初的问题。 –

+0

永远不要在后台线程上进行UI更新 - 始终在主线程上。 –

1

经过一些更多的调试后,我得到了一个错误,当项目减少(而不是像我上面试过的增加)。

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

这导致MED到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() 

这解决了我的问题。

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

相关问题