AFNetworkings setImageWithURLRequest displays wrong image for UITableViewCell

My method tableView:cellForRowAtIndexPath

uses the following code. I am using AFNetworking setImageWithURLRequest

to display an image for a cell. However, sometimes the wrong images are placed in cells in my TableView.

I believe the error is when the success block is executed and the object contact

no longer matches the loaded image.

// ... init/deque ...
/* CAN BE EITHER USER OR CONTACT DETAILS */
id contact = [[self.connections objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];

if ([contact isKindOfClass:[User class]]) {
    [cell configurateCellWithUser:(User *)contact];
    if(((User*)contact).avatar_name.length > 0){
        [cell.profilePicture setImageWithURLRequest:[self getURLRequestForUser:contact]
                                   placeholderImage:[UIImage imageWithData:((User*)contact).avatar_image]
                                            success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {

                                                cell.profilePicture.image = image;                                                       
                                                ((User*)contact).avatar_image = UIImageJPEGRepresentation(image, 0.01);
                                            } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
                                                cell.profilePicture.image = [UIImage imageNamed:@"avatar_contact-card"];

                                            }];
    }else {
        [cell.profilePicture setImage:[UIImage imageNamed:@"avatar_contact-card"]];
    }
} else {
    [cell.profilePicture setImage:[UIImage imageNamed:@"avatar_contact-card"]];
    [cell configurateCellWithContactDetails:(ContactDetails *)contact];
}
return cell;

      

How can I fix this and display the appropriate image for the correct cell?

+3


source to share


2 answers


You probably did not properly prepare your table cell for reuse. This can cause you to see old images when image requests fail to reach their UIImageView receiver due to reuse or network request failure. It is also possible that URL requests will fail, causing the correct image to be overwritten by a previous network request, which may be delayed.

You need to make sure you clear the profilePicture

imageView and discard any pending url requests that might later change the image.

Place the following code in your UITableViewCell subclass:



- (void)prepareForReuse {
    [self cancelImageRequestOperation];
    self.profilePicture.image = nil;
}

      

Make sure you import the UIImageView + AFNetworking category into your UITableViewCell:

#import UIImageView+AFNetworking.h

      

+1


source


Try to set cell.profilePicture.image

at nil

the beginning of your tableView:cellForRowAtIndexPath:

cell after deleting.



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    Cell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    cell.profilePicture.image = nil;    

    //the rest of your code.
}

      

0


source







All Articles