Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I implement `prepareForReuse`?

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?

like image 510
user4334509 Avatar asked Jan 13 '15 18:01

user4334509


1 Answers

Try adding this to your MGSwipeTableCell.m:

-(void)prepareForReuse
{
    [super prepareForReuse];

    self.textLabel.text = @"";
    self.detailTextLabel.text = @"";
    self.imageView.image = nil;
} 
like image 134
Mike Taverne Avatar answered Sep 29 '22 05:09

Mike Taverne