不调用从NIB加载的自定义单元的Dealloc

时间:2012-08-20 09:06:15

标签: iphone uitableview memory-management

所以我有一个名为MCProductCell的子类UITableViewCell,它是从NIB加载的。问题是当表被释放时,我的自定义单元格的dealloc方法甚至不会被调用一次。

以下是一些示例代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"MCProductCellIdentifier";
    MCProductCell *cell = (MCProductCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
   // Boolean value needed to determine if it is a reused cell or not. If it's not reused we have 
   // to start the thread that loads the image. For reused cells, that thread is started at the
   // end of the scrolling
   BOOL recycled = YES;
   if (cell == nil) {
       NSLog(@"cell alloc");
       recycled = NO;
       NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"MCProductCell" owner:self options:nil];
       cell = [nib objectAtIndex:0];
   }
   MCProduct *product = [products objectAtIndex:indexPath.row];
   cell.product = product;
   cell.cartViewController = self;
   cell.productImage = product.cachedThumbnailImage;
   if (product.cachedThumbnailImage == nil) {
       cell.productImage = [ViewControllerUtils getDefaultImage];
       if (!recycled)
           [NSThread detachNewThreadSelector:@selector(loadImage:) toTarget:cell withObject:cell.product.imageThumbnailUrl];
   }

   return cell;

}

由于某种原因,当我第一次展示包含该表的UIViewController时,我的自定义单元格的dealloc方法称为ONCE。 问题是在dealloc方法中我想要将单元格作为观察者移除,如果没有调用它,则不会将该单元格作为观察者移除。 桌面视图也是一个出口。

2 个答案:

答案 0 :(得分:0)

我想通了,一定是因为单元格的保留计数不会降到0。 这意味着你有另一个保留。

我更有经验的同事认为它是因为你正在使用detachNewThreadSelector,它可能会保留单元格。

他建议您使用某种类型的异步图像来加载图像,例如 https://github.com/nicklockwood/AsyncImageView/

祝你好运。

答案 1 :(得分:0)

如何定义'cell.cartViewController'属性?如果它保留了你的控制器对象(self),那么你可能在那里有一个保留周期!

相关问题