在 Objective C 中动态修改 UICollectionViewCell 中单元格的高度
Dynamically modifying the height of cells in a UICollectionViewCell in Objective C
我需要制作一个 Pinterest 风格的新闻源。我创建了一个包含 2 列的集合视图。我正在 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])
}];
在 -setImageWithURLRequest:...
中,当图像加载到哈希 table 中时,您应该使用当前图像路径调用 -reloadItemsAtIndexPaths:
。这将重新加载单元格,从而使 CollectionView 检查单元格的大小。
您可以尝试的另一个解决方案是在下载图像时使集合视图布局无效。
- (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) {
[self.collectionView.collectionViewLayout invalidateLayout];
}];
return cell;
}
在此之后 return 委托方法中的适当大小:
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return /* size */;
}
我在开发类似于 pinterest 布局的东西时遇到了类似的问题。
即使我们在异步下载图像时也有延迟,一旦下载完成,逐个单元地刷新会让用户感到震惊,并会导致 UI 混乱。
我建议的解决方案是,
比如post是用图片创建的时候,计算图片的尺寸(宽高),保存到数据库中。因此在下载或缓存图像之前图像的大小是可用的,我们可以将单元格缩放到图像的高度并显示默认图像,当实际图像被缓存或下载时它会默默地替换默认图像而不用任何UI 混蛋。
如果您使用像 cloudinary 这样的服务,那么您可以避免存储图像大小(高度和宽度)的痛苦。 cloudinary api 会给你图像的高度和宽度。
我需要制作一个 Pinterest 风格的新闻源。我创建了一个包含 2 列的集合视图。我正在 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])
}];
在 -setImageWithURLRequest:...
中,当图像加载到哈希 table 中时,您应该使用当前图像路径调用 -reloadItemsAtIndexPaths:
。这将重新加载单元格,从而使 CollectionView 检查单元格的大小。
您可以尝试的另一个解决方案是在下载图像时使集合视图布局无效。
- (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) {
[self.collectionView.collectionViewLayout invalidateLayout];
}];
return cell;
}
在此之后 return 委托方法中的适当大小:
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
return /* size */;
}
我在开发类似于 pinterest 布局的东西时遇到了类似的问题。
即使我们在异步下载图像时也有延迟,一旦下载完成,逐个单元地刷新会让用户感到震惊,并会导致 UI 混乱。
我建议的解决方案是,
比如post是用图片创建的时候,计算图片的尺寸(宽高),保存到数据库中。因此在下载或缓存图像之前图像的大小是可用的,我们可以将单元格缩放到图像的高度并显示默认图像,当实际图像被缓存或下载时它会默默地替换默认图像而不用任何UI 混蛋。
如果您使用像 cloudinary 这样的服务,那么您可以避免存储图像大小(高度和宽度)的痛苦。 cloudinary api 会给你图像的高度和宽度。