8
votes

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)

7
Can you be a bit more specific about what is not working, there is no way if you have everything set up correctly that calling reloadData() on collectionView does not call cellForItemAtIndexPath for each visible item (row)...Ladislav

7 Answers

10
votes

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.

2
votes

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.

2
votes

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()
        }
    
1
votes
var paths: [IndexPath] = []
if !data.isEmpty {
    for i in 0...salesArray.count - 1 {
        paths.append(IndexPath(item: i, section: 0))
    }
    collectionView.reloadData()
    collectionView.reloadItems(at: paths)
}

This is the code helped me, for solving this kind of issue.

0
votes

Post your code.

One possibility is that you are using `URLSession and trying to tell your collection view to update from the it's delegate method/completion closure, not realizing that that code is run on a background thread and you can't do UI calls from background threads.

0
votes

I faced the same problem with self resizing cells and above solution works as well but collection view scroll position resets and goes to top. Below solution helps to 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()
0
votes

i was facing also this kind of issue so finally i am able to solved using this line of code

[self.collectionView reloadData];

[self.collectionView reloadItemsAtIndexPaths:@[[NSIndexPath indexPathWithIndex:0]]];