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) {}];