ios – 如何实现`prepareForReuse`?

前端之家收集整理的这篇文章主要介绍了ios – 如何实现`prepareForReuse`?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在我的UITableViewCell中设置一个用sdwebimage下载的图像.为了保持UITableView不显示错误的图像,我需要在prepareForReuse中将图像设置为nil.但是,我在实现这个方面遇到了一些麻烦.

这是我设置图像的地方:

  1. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  2. {
  3. static NSString * reuseIdentifier = @"programmaticCell";
  4. MGSwipeTableCell * cell = [self.tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
  5. if (!cell) {
  6. cell = [[MGSwipeTableCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier];
  7. }
  8.  
  9. CGFloat brightness = [UIScreen mainScreen].brightness;
  10.  
  11. cell.textLabel.text = [self.streams[indexPath.row] name];
  12. cell.detailTextLabel.text = [self.streams[indexPath.row] published];
  13.  
  14. NSString *imageUrl = [NSString stringWithFormat: @"%@",[self.streams[indexPath.row] photo]];
  15.  
  16. NSLog(@"Image is: %@ and path is: %d",imageUrl,indexPath.row);
  17.  
  18. [cell.imageView sd_setImageWithURL:[NSURL URLWithString:imageUrl]
  19. placeholderImage:[UIImage imageNamed:@"tile-blue.png"] options:indexPath.row == 0 ? SDWebImageRefreshCached : 0];
  20.  
  21. cell.delegate = self; //optional
  22.  
  23. return cell;
  24. }

当我实现 – (void)prepareForSegue Xcode不断抛出错误,说单元格不可用.实现这个的正确方法是什么?

解决方法

尝试将此添加到您的MGSwipeTableCell.m:
  1. -(void)prepareForReuse
  2. {
  3. [super prepareForReuse];
  4.  
  5. self.textLabel.text = @"";
  6. self.detailTextLabel.text = @"";
  7. self.imageView.image = nil;
  8. }

猜你在找的iOS相关文章