问题
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.
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.
How can I do this?
回答1:
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
Delegate Property
@property (strong, nonatomic) id<CellDelegate>delegate;
In Cell, when image finished call:
[self.delegate imageIsReadyForCell:self];
Then in your file hosting the collectionView in interface
@interface SomeViewController : UIViewController <CellDelegate>
And in the .m
- (void) imageIsReadyForCell:(id)cell {
NSIndexPath * indexOfReadyCell = [yourCollectionView indexPathForCell:cell];
[myCollectionView reloadItemsAtIndexPaths:@[indexPathOfReadyCell]];
}
回答2:
Not an answer but I don't have enough reputation to comment.
Im trying to update my collectionviewcell and I have put under the image load the:
[self.delegate imageReadyForCell:cell]
But this does not see to change anything, I have put breakpoints inside the method of imageReadyForCell but it doesn't even reach it. I followed all the steps as done above with the protolcol and property for the delegate of CellDelegate and I haven't managed to get this working.
来源:https://stackoverflow.com/questions/22285829/how-to-update-size-of-cells-in-uicollectionview-after-cell-data-is-set