objective-c - 如果没有来自API的图像,需要重新对齐Table View Cell

标签 objective-c if-statement sdwebimage

我正在使用 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/

相关文章:

java - 为什么在 JAVA 中允许一个 IF block 位于另一个没有大括号的 IF 中

bash - 如何制作 "if not true condition"?

ios - 从本地库传递图像的 url

ios - 检查 collectionView 是否滚动到顶部

iphone - iPhone 和 Xcode 4 的压缩/解压库

if-statement - 如何检查服务是否通过批处理文件运行,如果服务未运行则将其停止?

ios - 如何使用 SDWebImage 调整下载的图像大小?

ios - 如何解决此错误 : "Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)"?

ios - UITableView 半粘性 tableView Header 类似 App Store 详细 View

objective-c - 当函数的类不继承自 NSObject 时,从 ObjC 调用一个 swift 实例函数