Objective c 如果没有来自API的图像,则需要重新对齐表视图单元格
我正在使用SDWebImage。我正在从web服务API中正确地提取图像,但是如果从中获取响应的API没有图像(Objective c 如果没有来自API的图像,则需要重新对齐表视图单元格,objective-c,if-statement,sdwebimage,Objective C,If Statement,Sdwebimage,我正在使用SDWebImage。我正在从web服务API中正确地提取图像,但是如果从中获取响应的API没有图像(“null”),我希望重新对齐我的表视图单元格 视图控制器.m [cell.imageView setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", imageURL]] placeholderImage:[UIImage imageNamed:@"placeholder"]]; WebListC
“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);
}
}
我将此作为一般指南:
(我的占位符图像现在只有1px乘1px)
所以基本上我的问题是,当没有图像并且我想重新对齐我的表视图单元格时,我找不到一个好的“if”语句
关于简单的“如果”语句有什么建议吗
编辑:
现在就使用这个代码,这是有效的,除了我得到一个警告说“在这个块中捕获单元格很可能导致一个保留周期”
尝试使用该块检查图像检索是否成功。并将弱引用添加到单元格:
从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"]
];
}
}];
你为什么要拿1作比较?@Carlvaezey我一直在关注我链接的教程,尽管它不适合那个特定的部分。所以这部分代码是错误的,我试着用这种方式标记它,因为我觉得最好把我为它想出的所有东西都发布出来,而不是什么都不发布。我最初的想法是检查它是否是占位符图像(1px x x 1px),但我意识到这样做是不正确的。谢谢你的检查,谢谢你的回复!这看起来像是我要找的那种东西。我是新来的,那么你建议什么类型的“完成代码”呢?@Reez更新了我的答案。如果需要,您还可以检查错误变量,以便进行更健壮的检查。太棒了,这看起来很棒。不过,现在我要在一个名为WebListCell
的子类中分别设置当前单元格的样式,那么我应该将该代码移到您提供的代码中吗?我看到您为我提供了一个重新对齐表格视图单元格的位置,只是不确定我是否应该为一个条件对齐那里的表格视图单元格,并为不同的条件对齐WebListCell
中的表格视图单元格。你觉得怎么样?@Reez我想这要看情况了。如果您将在WebListCell中重复使用对齐作为一个条件,我认为您可以将其保留在那里。这对我来说现在非常有用,除了我收到的警告(显示在上面的“编辑”代码中),它显示在块内代码的第一行。。。有没有办法让它不出现?再次感谢-
// 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"]
];
}
}];