2

我正在使用 SDWebImage。我从 Web 服务 API 中正确提取图像,如果我从中获取响应的 API 没有图像 ( "null"),我想重新对齐我的表视图单元格。

查看控制器.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/

(我现在的占位符图像只有 1px x 1px)

所以基本上我的问题是当没有图像并且我想重新对齐我的表格视图单元格时,我找不到一个好的“if”语句。

关于简单的“if”语句有什么建议吗?

编辑: 现在使用此代码,它有效,除了我收到一条警告说“在此块中强烈捕获cell可能会导致保留周期”

[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"]
                                   ];
                              }
                          }];
4

1 回答 1

3

尝试使用块来检查图像检索是否成功。并且还添加了对单元格的弱引用:Fix warning "Capturing [an object] strong in this block is likely to lead to a retain cycle" in ARC-enabled code

从 SDWebImage github 页面:

使用块,您可以收到有关图像下载进度以及图像检索是否成功完成的通知:

// 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"]
        ];
    }
}];
于 2013-05-23T06:11:46.080 回答