How to update size of cells in UICollectionView after cell data is set?

十年热恋 提交于 2020-01-02 16:39:02

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!