设置单元格数据后如何更新 UICollectionView 中的单元格大小? [英] How to update size of cells in UICollectionView after cell data is set?
问题描述
所以我有一个 UICollectionView,每个单元格中都有不同大小的不同图像.当 cellForItemAtIndexPath: 被调用时,我使用一种方法更新 UICollectionViewCell,该方法在网络上异步获取图像并以全尺寸显示.
So I have a UICollectionView with a different image of different size in each cell. When cellForItemAtIndexPath: is called, I update the UICollectionViewCell with a method that fetches an image asynchronously on the web and displays it full size.
我的问题是我的单元格的大小已经预先设置了 sizeForItemAtIndexPath.我希望它被召回,以便单元格具有下载的图像大小.
My problem is the size of my cell is already set prior with sizeForItemAtIndexPath. I would like it to be recalled so the cell has the downloaded image size.
我该怎么做?
推荐答案
如果您已经设置了大小,请找到要刷新的单元格的索引:
If you have already set up your sizing, find the index of the cell you'd like to refresh:
NSIndexPath * indexPathOfCellToResize = // index path of cell to refresh
[myCollectionView reloadItemsAtIndexPaths:@[indexPathOfCellToResize]];
可能有更好的方法来做到这一点,但这里有一些有效的方法:
There's probably a better way to do this, but here's something that works:
在您的单元中创建委托协议
Create A Delegate Protocol In Your Cell
@protocol CellDelegate
- (void) imageIsReadyForCell:(id)cell;
@end
委托财产
@property (strong, nonatomic) id<CellDelegate>delegate;
在 Cell 中,当图像完成调用时:
In Cell, when image finished call:
[self.delegate imageIsReadyForCell:self];
然后在您的文件中托管接口中的 collectionView
Then in your file hosting the collectionView in interface
@interface SomeViewController : UIViewController <CellDelegate>
在.m
- (void) imageIsReadyForCell:(id)cell {
NSIndexPath * indexOfReadyCell = [yourCollectionView indexPathForCell:cell];
[myCollectionView reloadItemsAtIndexPaths:@[indexPathOfReadyCell]];
}
这篇关于设置单元格数据后如何更新 UICollectionView 中的单元格大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!