0

I have a UICollectionViewController that is also acts as UICollectionViewDelegateFlowLayout to size cells. One cell selection modally presents some custom controls and then returns when completed, but the selected cell is no longer selected when it reappears.

I see delegate method for collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) being called, but not collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath). I also see the layout being asked for the preferred cell size, so I’m thinking they are being redrawn, but the cells already exist.

I can solve the problem by calling relaodData(), and the cell knows it is selected, and is redrawn accordingly, but that seems like a costly and wrong solution. Any suggestions about what I am missing? Thanks.

Yarn
  • 238
  • 1
  • 12

4 Answers4

0

collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) being called, but not collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath)

0

The default implementation of the following delegate method deselects the cell.

Override it without calling super:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
ukim
  • 2,395
  • 15
  • 22
  • I override this function to modally present the view controller that ultimately causes the problem. So if I leave the implementation empty, the offending view controller is never presented. Is there a better method than this one to present a view controller when a certain cell is selected? – Yarn Aug 10 '18 at 20:59
  • @Yarn, I wanted to mean do not call super, I will modify my answer. – ukim Aug 10 '18 at 21:01
0

I see delegate method for collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) being called, but not collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath). I also see the layout being asked for the preferred cell size, so I’m thinking they are being redrawn, but the cells already exist.

0

Figured it out.

UICollectionViewController has property clearsSelectionOnViewWillAppear that is automatically set to true.

All I had to do was set it to false.

Yarn
  • 238
  • 1
  • 12