我需要制作一个 Pinterest 风格的新闻源。我创建了一个包含 2 列的 Collection View 。我正在 cellForItemAtIndexPath 中异步下载照片:
- (UICollectionViewCell *)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(NSIndexPath*)indexPath {
TFNewsObject *obj = [self.sortedDataSource objectAtIndex:indexPath.row];
[imageView sd_setImageWithURL:url placeholderImage:nil options:SDWebImageRefreshCached
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
obj.tfImage = image;
}];
return cell;
}
问题是我需要在 heightForItemAtIndexPath 中设置单元格的大小,但图像尚未下载,因为它尚未进入 cellForItemAtIndexPath。我将高度设置为 100 的固定大小。 在 cellforItemAtIndexPath 中下载图像后,我需要以某种方式触发 heightForItemAtIndexPath。
我尝试过的另一件事是同步下载 heightForItemAtIndexPath 中的图像,并在下载图像后计算高度。通过这种方式,它可以根据图像正确显示每个单元格的不同高度,但需要永远加载所有图像。
下载图像后是否可以修改单元格的高度?谢谢!
最佳答案
试试这个
[imageView sd_setImageWithURL:url placeholderImage:nil options:SDWebImageRefreshCached
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
obj.tfImage = image;
collectionView .reloadItems(at: [indexPath])
}];
关于ios - 在 Objective C 中动态修改 UICollectionViewCell 中单元格的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43935195/