views:

35

answers:

1

Hello stack overflow,

So I have a problem and know exactly what is causing it, just not how to fix it. Basically I have a tableViewCell that is loading something like this:

BroadcastTableViewCell *cell = (BroadcastTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

/*
 * Other code to finish cell setup
 *
 */

// Problem section
CGRect frame;
    frame.size.width=50; frame.size.height=70;
    frame.origin.x=0; frame.origin.y=0;
    AsyncImageView* asyncImage = [[[AsyncImageView alloc]
                                   initWithFrame:frame] autorelease];
    asyncImage.tag = 999;
    NSURL *url = [[[NSURL alloc] initWithString:[NSString stringWithFormat:@"http://image URL/%@", trimmedPath]] autorelease];      
    [asyncImage loadImageFromURL:url];
    [cell.contentView addSubview:asyncImage];

Basically this is a way to asynchronously load the cell images so the tableview scrolls smoothly. The problem is, when I dequeue a new cell with the existing ID, the image does not get reset and is placed under the new one. Does anyone know how to detect that image and remove it when a new cell is dequeued?

+1  A: 

Just search the subviews for your AsyncImageView using viewWithTag:

AsyncImageView* asyncImage = [cell.contentView viewWithTag:999];
if (asyncImage == nil) {
  CGRect frame;
  frame.size.width=50; frame.size.height=70;
  frame.origin.x=0; frame.origin.y=0;
  asyncImage = [[[AsyncImageView alloc] initWithFrame:frame] autorelease];
  asyncImage.tag = 999;
  [cell.contentView addSubview:asyncImage];
} else {
  // might want to cancel download here somehow
}

NSURL *url = [[[NSURL alloc] initWithString:[NSString stringWithFormat:@"http://image URL/%@", trimmedPath]] autorelease];      
[asyncImage loadImageFromURL:url];
Jason Harwig
Brilliant, that actually makes perfect sense since that is the reason I am adding the tag in the first place XD Thanks for the help!
Geoff Baum
That will actually help with efficiency as well. :) Win/win.
Geoff Baum