BHendricks
BHendricks

Reputation: 4493

UICollectionView cellForItemAtIndexPath not called after numberOfItemsInSection returns nonzero item number

Is there any reason that cellForItemAtIndexPath would not get called after numberOfItemsInSection gets called and returns a nonzero number? Would a reloadData from a completion block affect this? I'm running into an issue in which I am trying to delete a cell through a UIAlertController and the UI looks great, but when I go to reloadData after the UI animation finishes numberOfItemsInSection properly gets called with the right number of elements (the previous number of elements minus 1), yet cellForItemAtIndexPath does not get called, so my cells don't get reloaded. Any ideas as to why this would happen?

This is on Swift 1.2, XCode 6.4, and any help would be greatly appreciated

Just to give a bit more information, the method I'm using to delete a cell is looking a little like:

self.collectionView.performBatchUpdates({
    let indexPathCellToDelete = self.indexPath(feedCell)
    self.collectionView.deleteItemsAtIndexPaths([indexPathCellToDelete])
}, completion:nil)

Would having a second reload in the completion block change anything?

Upvotes: 3

Views: 3125

Answers (1)

BHendricks
BHendricks

Reputation: 4493

Turns out performBatchUpdates, according to UICollectionView Performing Updates using performBatchUpdates needs to be called after the change to my data source has been done. The issue was, though, that my method for deleting an item from my data source itself reloads the collection view, so there was an extra reload that did not need to happen, which was causing the issue, it seems. I initially had the call to my data source deletion method in the completion block of the performBatchUpdates, but moving that above and just reloading the entire collection view seems to have solved the issue. Seems like a weird race condition in my case where there are multiple reloads happening simultaneously and cellForItemAtIndexPath didn't like that.

Upvotes: 2

Related Questions