What is wrong if cellForItemAtIndexPath: is called in didSelectItemAtIndexPath:

468 Views Asked by At

I called cellForItemAtIndexPath: in the didSelectItemAtIndexPath: method. The result was that didSelectItemAtIndexPath: was only called one time per cell. So I can tap the first time on a cell, then I can tap on another cell, but if I tap on the first cell again the didSelectItemAtIndexPath is not called anymore (but all other "new" cells).

Now I'm not calling cellForItemAtIndexPath: anymore and now didSelectItemAtIndexPath: works as expected. But why I'm not allowed to do this? Any reasons for this?

1

There are 1 best solutions below

0
On BEST ANSWER

The collection view is caching instances of cells for you and you dequeue them when you create the cell at an index path. The collection view is making some assumptions about what's going on when you do this, so if you start making multiple requests for cells at the same index path strange things start to happen. Sometimes this results in visual issues, sometimes interaction issues.

If you want to get the cell at a specified index path you should ask the collection view directly instead of using your own delegate method, this will give you the existing cell on screen rather than a new cell.