如果没有来自API的Image,则需要重新对齐Table View Cell

时间:2013-05-23 02:57:57

标签: objective-c if-statement sdwebimage

我正在使用SDWebImage。我正在从网络服务API正确提取图片,如果我收到回复的API没有图片("null"),我想重新调整我的表视图单元格。

查看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/

(我的占位符图片现在只是1px xpp)

所以基本上我的问题是我找不到一个好的“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"]
                                   ];
                              }
                          }];

1 个答案:

答案 0 :(得分:3)

尝试使用该块检查图像检索是否成功。并且还添加了对单元格的弱引用:Fix warning "Capturing [an object] strongly 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"]
        ];
    }
}];