我正在使用 SDWebImage。我正在从网络服务 API 中正确提取图像,但是如果我从中获取响应的 API 没有图像(“null”
) ,我想重新对齐我的 TableView 单元格。
View Controller .m
[cell.imageView setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", imageURL]] placeholderImage:[UIImage imageNamed:@"placeholder"]];
WebListCell.m
- (void)layoutSubviews {
[super layoutSubviews];
self.headlineLabel.frame = CGRectMake(134, 12.5, 130, 50);
self.descriptionLabel.frame = CGRectMake(134, 65, 130, 50);
self.imageView.frame = CGRectMake(12, 15, 96, 54);
//This Part Not Working
float limgW = self.imageView.image.size.width;
if (limgW == 1) {
self.headlineLabel.frame = CGRectMake(15, 15, 250, 50);
self.descriptionLabel.frame = CGRectMake(15, 65, 250, 50);
self.imageView.frame = CGRectMake(2, 2, 2, 2);
}
}
我用它作为一般指南: http://www.wrichards.com/blog/2011/11/sdwebimage-fixed-width-cell-images/
(我的占位符图像现在只有 1 像素 x 1 像素)
所以基本上我的问题是,当没有图像并且我想重新对齐我的 TableView 单元格时,我找不到一个好的“if”语句。
关于简单的“if”语句有什么建议吗?
编辑:
现在使用此代码,它可以工作,但我收到一条警告,提示“在此 block 中强烈捕获单元格
可能会导致保留周期”
[cell.imageView setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", imageURL]]
placeholderImage:[UIImage imageNamed:@"placeholder.png"]
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
if(image == nil) {
//realign your table view cell
[cell.imageView setImageWithURL:[NSURL URLWithString:@"http://website.com/image1"]
placeholderImage:[UIImage imageNamed:@"placeholder.png"]
];
}
}];
最佳答案
尝试使用 block 来检查图像检索是否成功。并且还添加了对单元格的弱引用:Fix warning "Capturing [an object] strongly in this block is likely to lead to a retain cycle" in ARC-enabled code
来自 SDWebImage github 页面:
通过 block ,您可以收到有关图像下载进度以及图像检索是否成功完成的通知:
// Here we use the new provided setImageWithURL: method to load the web image
__weak UITableViewCell *wcell = cell;
[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) {
if(image == nil) {
//realign your table view cell
[wcell.imageView setImageWithURL:[NSURL URLWithString:@"http://website.com/image1"]
placeholderImage:[UIImage imageNamed:@"placeholder.png"]
];
}
}];
关于objective-c - 如果没有来自API的图像,需要重新对齐Table View Cell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16704942/