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