Brandon M
Brandon M

Reputation: 369

UICollectionViewCell unavailable in sizeForItemAt

I'm trying to animate a cell size change in a collection view.

In 'didSelectItemAt':

            collectionView.performBatchUpdates({
                collectionView.collectionViewLayout.invalidateLayout()
                let cell = collectionView.cellForItem(at: indexPath)
                let frame = cell?.frame
                cell?.frame = CGRect(x: (frame?.origin.x)!, y: (frame?.origin.y)!, width: (frame?.size.width)! + 100, height: (frame?.size.height)!)
            }, completion: nil)

That should set the size of the cell, but nothing happens. Clicking the cell a second time, I see that the size of the cell is retained to the size+100 (disregard the fact that it would grow every time - just trying to get it to work right now).

In the sizeForItemAt function, any attempt to retrieve the cell returns nil.

collectionView.numberOfItems(inSection: indexPath.section)

returns 3, but

collectionView.cellForItem(at: indexPath)

returns nil. Why?

How can I get the cell for that collection view with the given index path in the scope of sizeForItemAt:???

Upvotes: 1

Views: 419

Answers (2)

Andrew McKinley
Andrew McKinley

Reputation: 1137

Are sure indexPath.row < 3 ? Because if not then it will return nil. Put a breakpoint in cellForItem and see what happens.

Upvotes: 0

Brandon M
Brandon M

Reputation: 369

Apparently sizeForItemAt gets called before cellForItem.

Upvotes: 1

Related Questions