Resize UICollectionView cells after image inside has been downloaded

前端 未结 3 1215
你的背包
你的背包 2020-12-24 00:59

I\'m building a UICollectionView and my custom cells will contain two labels and one image.

Each image is downloaded asynchronously so I don\'t know it\

3条回答
  •  执念已碎
    2020-12-24 01:08

    You should just be able to invalidate your collection view layout in an animation block when the image comes back. Things might get a little complicated if more than one image finishes completion at once, but this should work:

    [cell.imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
                   placeholderImage:[UIImage imageNamed:@"placeholder.png"]
                          completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType)^{
                              [UIView animateWithDuration:0.3f animations:^{
                                  [self.collectionView.collectionViewLayout invalidateLayout];
                              }];
                          }];
    

    Then just return a different size in the appropriate delegate method.

    - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
        return /* a different size if the image is done downloading yet */;
    }
    

提交回复
热议问题