Custom UITableViewCell Removing a subview
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 fini开发者_如何学Pythonsh 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?
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];
精彩评论