1

I want to change size of CollectionViewCell when cell is clicked. Cell needs to expand or collapse if it is opened already. I am doing it so far with this code inside didSelectItemAtIndexPath method:

UICollectionViewCell *myCell = [collectionView cellForItemAtIndexPath:indexPath];

[UIView transitionWithView:collectionView duration:.3 options:UIViewAnimationOptionCurveLinear
     animations:^{
        CGRect oldFrame = myCell.frame;
        if ([expandedCell isEqual:myCell]) {
            expandedCell = nil;
            oldFrame.size.height = 42;
        }else{
            expandedCell = myCell;
            oldFrame.size.height = 220;
        }
            myCell.frame = oldFrame;

   } completion:^(BOOL finished) {}];

When I expand last cell it works fine. It collapses fine too. But when I click on cell at index 0 for example, frame of cell 0 is going under frame of cell 1. How can I change frames of all cells after selected index path? Don't forget, I need animation while changing all cell frames.

EDIT: I found answer on my question with next few lines of code. Point is that you need to call reload data inside animation so that changing size inside sizeForItemAtIndexPath will be animated.

[self.collectionView performBatchUpdates:^{
    [self.collectionView reloadData];
} completion:^(BOOL finished) {}];
AleksandarNS
  • 265
  • 1
  • 2
  • 15

1 Answers1

-1

You need to resize the height in collection view height delegate and reload that particular cell in didSelectItem

Deepak Khiwani
  • 734
  • 1
  • 8
  • 33