7

I'm setting an image, downloaded with sdwebimage, in my UITableViewCell. In order, to keep the UITableView from displaying wrong images, I need to set the image to nil in prepareForReuse. However, I'm having some trouble implementing this.

This is where I set the image:

- (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;
}

When I implement - (void)prepareForSegue Xcode keeps throwing errors saying that cell is unavailable to it. What is the proper way to implement this?

user4334509
  • 125
  • 2
  • 10
  • 1
    `prepareForReuse` is implemented on your `MGSwipeTableCell`. Here, the reference to the cell is called `self` instead of `cell`. – Ian MacDonald Jan 13 '15 at 18:55
  • But if I do `self.textLabel = nil`, it throws `Property 'textLabel' not found on object of type 'ViewController'` – user4334509 Jan 13 '15 at 19:16
  • 1
    You have mistaken `prepareForSegue` on your `ViewController` as the implementation of `prepareForReuse` on your `MGSwipeTableCell`. Go to your `@implementation MGSwipeTableCell` and implement `prepareForReuse` there. – Ian MacDonald Jan 13 '15 at 19:29
  • Your Q is not clear. What is the error you see? Where is the code where the error is raised? – Wain Jan 13 '15 at 19:44
  • Also see [What is the correct way to use prepareForReuse?](https://stackoverflow.com/questions/40773208/what-is-the-correct-way-to-use-prepareforreuse/47514477#47514477) – mfaani Nov 27 '17 at 15:34

1 Answers1

9

Try adding this to your MGSwipeTableCell.m:

-(void)prepareForReuse
{
    [super prepareForReuse];

    self.textLabel.text = @"";
    self.detailTextLabel.text = @"";
    self.imageView.image = nil;
} 
Mike Taverne
  • 9,156
  • 2
  • 42
  • 58
  • 8
    As a comment to the solution above, which Mike likely knows (was just demo code): Don't implement prepareForReuse to reset the content of a cell. It should only reset the appearance. From the docs: "For performance reasons, you should only reset attributes of the cell that are not related to content, for example, alpha, editing, and selection state.". Further: "The table view's delegate in tableView:cellForRowAtIndexPath: should always reset all content when reusing a cell". Set content like text in cellForRowAtIndexPath, reset appearance in ```prepareForReuse```. – Frederik Winkelsdorf Apr 03 '16 at 16:40
  • Thanks for that explanation. I actually did not know that. I will keep that in mind in future. Typically I set content in cellForRowAtIndexPath but sometimes reset content in prepareForReuse. – Mike Taverne Apr 03 '16 at 21:01
  • 3
    You're very welcome! I stumbled upon this not long ago. It's good to keep in mind when trying to get the best performance out of the UITableView. – Frederik Winkelsdorf Apr 03 '16 at 23:19