我正在我的UITableViewCell中设置一个用sdwebimage下载的图像.为了保持UITableView不显示错误的图像,我需要在prepareForReuse中将图像设置为nil.但是,我在实现这个方面遇到了一些麻烦.
这是我设置图像的地方:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static Nsstring * reuseIdentifier = @"programmaticCell";
MGSwipeTableCell * cell = [self.tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
if (!cell) {
cell = [[MGSwipeTableCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier];
}
CGFloat brightness = [UIScreen mainScreen].brightness;
cell.textLabel.text = [self.streams[indexPath.row] name];
cell.detailTextLabel.text = [self.streams[indexPath.row] published];
Nsstring *imageUrl = [Nsstring stringWithFormat: @"%@",[self.streams[indexPath.row] photo]];
NSLog(@"Image is: %@ and path is: %d",imageUrl,indexPath.row);
[cell.imageView sd_setimageWithURL:[NSURL URLWithString:imageUrl]
placeholderImage:[UIImage imageNamed:@"tile-blue.png"] options:indexPath.row == 0 ? SDWebImageRefreshCached : 0];
cell.delegate = self; //optional
return cell;
}
当我实现 – (void)prepareForSegue Xcode不断抛出错误,说单元格不可用.实现这个的正确方法是什么?
解决方法
尝试将此添加到您的MGSwipeTableCell.m:
-(void)prepareForReuse
{
[super prepareForReuse];
self.textLabel.text = @"";
self.detailTextLabel.text = @"";
self.imageView.image = nil;
}