自定义UITableViewCell删除子视图

时间:2010-10-19 14:08:28

标签: iphone objective-c ipad ios

所以我遇到了问题并确切知道是什么导致了它,而不是如何解决它。基本上我有一个tableViewCell加载这样的东西:

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];

基本上这是一种异步加载单元格图像的方法,因此tableview可以平滑滚动。问题是,当我使用现有ID对新单元格进行出列时,图像不会被重置并置于新单元格下。有没有人知道如何检测该图像并在新细胞出列时将其删除?

1 个答案:

答案 0 :(得分:1)

只需使用AsyncImageView

搜索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];