0

したがって、NIB からロードされる MCProductCell という名前のサブクラス UITableViewCell があります。問題は、テーブルが解放されると、カスタム セルの dealloc メソッドが 1 回も呼び出されないことです。

サンプルコードは次のとおりです。

- (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 メソッドでセルをオブザーバーとして削除したいことです。それが呼び出されない場合、セルはオブザーバーとして削除されません。また、テーブルビューはアウトレットです。

4

2 に答える 2

0

I figured out, it must be because the retain count of the cell is not going down to 0. Which means you have another retain.

My more experienced colleague thinks its because you are using the detachNewThreadSelector, which probably retains the cell.

He suggested you would load the image by using some type of asynchrony image such as https://github.com/nicklockwood/AsyncImageView/

Good luck.

于 2012-08-20T09:13:22.323 に答える
0

「cell.cartViewController」プロパティはどのように定義されていますか? コントローラー オブジェクト (自己) を保持している場合は、保持サイクルが含まれている可能性があります。

于 2012-08-20T09:57:27.443 に答える