我想在单击单元格时更改 CollectionViewCell
的大小。如果单元格已经打开,则需要展开或折叠。到目前为止,我在 didSelectItemAtIndexPath
方法中使用这段代码:
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) {}];
当我展开最后一个单元格时,它工作正常。它也很好地崩溃了。但是,例如,当我单击索引 0 处的单元格时,单元格 0 的框架位于单元格 1 的框架之下。如何在选定索引路径后更改所有单元格的框架?别忘了,我在更改所有单元格框架时需要动画。
编辑:我在接下来的几行代码中找到了我的问题的答案。重点是您需要在动画中调用重新加载数据,以便在 sizeForItemAtIndexPath
中更改大小将被动画化。
[self.collectionView performBatchUpdates:^{
[self.collectionView reloadData];
} completion:^(BOOL finished) {}];
最佳答案
您需要在 Collection View 高度委托(delegate)中调整高度,并在 didSelectItem
中重新加载该特定单元格
关于ios - 用动画改变 UICollectionViewCell 的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26585030/