Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UICollectionView reloadData not working

Its getting called in viewDidLoad, after fetching the data used. After some print debugging it looks like it calls all the appropriate delegeate methods, if no data is changed. If there has been some data changed, cellForItemAt does not get called.

Reloading the whole section works fine. But gives me an unwanted animation. Tried disabling UIView animation before, and enabling after reloading section, but still gives me a little animation.

collectionView.reloadSections(IndexSet(integer: 0)) 

Here is my current situation, when using reloadData()

The UICollectionViewController is a part of a TabBarController. I'm using custom UICollectionViewCells. The data is loaded from CoreData.

First time opening the tab, its works fine. After updating the favorites item in another tab, and returning to this collectionView, its not updated. But if i select another tab, and go back to this one, its updated.

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

Now my Item shows up in the list. You can see from the Get Cell that cellForItemAt is called aswell. Nothing has changed between these last two loggings, just clicked back/fourth on the tabs one time.

Forgot to mention, but this code IS working fine in the simulator. On a read device its just not giving me any error, just not showing the cell (or calling the cellForItemAt)

like image 729
Espen Birk Avatar asked Oct 14 '17 15:10

Espen Birk


3 Answers

After some more debugging i got an error when the items got reduced (instead of increasing like i've tried above).

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

This led med to 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.

I am using autoresizing cells, but i guess this does not explain why cellForItemAt did not get called.

like image 59
Espen Birk Avatar answered Oct 23 '22 02:10

Espen Birk


in my case, my collectionview was in a stackview, i didnt make any constraints, when i added the necessary constraints it worked.

Just check collectionview's constraints.

like image 21
Omar N Shamali Avatar answered Oct 23 '22 04:10

Omar N Shamali


Ok, maybe I can help someone =)

I do this:

  1. Load data from API
  2. Then after load data call DispatchQueue.main.async like this

        DispatchQueue.main.async {
            self.pointNews = pointNews
        }
    
  3. In self.pointNews didSet I try to do collectionView.reloadData(), but it work only, if I call DispatchQueue.main.async in didSet

        DispatchQueue.main.async {
            self.collectionView.reloadData()
        }
    
like image 2
Владимир Михайлов Avatar answered Oct 23 '22 03:10

Владимир Михайлов