Search code examples
xcodeuitableviewtableviewasyncimageview

Repeated image loading in tableviewcell


I have a tableview with each cell having its own imageview. I'm using AsyncImageView to load images from the internet into the imageview. Certain imageviews seem to be loading a repeated image into different cells:

enter image description here

In the screenshot above, Image B loads twice when it's not supposed to. This happens quite often as I scroll up and down. When this happens I scroll away and scroll back to the cell and the correct image is loaded. Later when I scroll back again, the same problem happens. I'm not able to trace where the problem is coming from. Anyone have any ideas?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath   
{
    static NSString *simpleTableIdentifier = @"CustomCell";

    CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:simpleTableIdentifier owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }else
    {
        [[AsyncImageLoader sharedLoader] cancelLoadingImagesForTarget:cell.logoImageView];
        cell.logoImageView.image = nil;
    }

    //Cause sections
    NSMutableArray *tempArray = [self.sectionArray objectAtIndex:indexPath.section];
    MyObject *myObject = [tempArray objectAtIndex:indexPath.row];

    NSString *thumbUrlString = [myObject valueForKey:@"thumbUrl"];

    if([thumbUrlString length]>0 )
    {
        NSString *imageUrl= [NSString stringWithFormat:@"http://www.sitename.com/%@",thumbUrlString];
        NSURL *url = [NSURL URLWithString:imageUrl];
        cell.logoImageView.imageURL=url;
    }else{
        cell.logoImageView.image = [UIImage imageNamed:@"placeholder.png"];
    }

    return cell;
}

Solution

  • Made a silly mistake with:

     cell.logoImageView.image = nil 
    

    Changed it to:

    cell.logoImageView.imageURL = nil