确定何时取消分配UITableViewCell

ran*_*dom 2 memory-management objective-c uitableview

我在我的应用程序中使用核心数据以及NSFetchedResultsController填充表格.我的数据库有40k +条目,因此表格相当长.每个表格单元格都有一个使用SDWebImage从Web加载的缩略图图像.如果我慢慢滚动,一切都很好,如果我在几秒钟内开始快速滚动,我就会崩溃.

NSZombies没有显示任何有用的东西.

我猜它与SDWebImage网络有关并且从网上加载.方法SDWebImage是在后台加载图像,然后在下载完成后设置下载的图像(罗嗦).我的想法是细胞被释放UITableView,然后SDWebImage尝试在解除分配的细胞上设置图像.因此,如果我可以确定何时UITableViewCell将要解除分配,我可以停止SDWebImage下载过程并希望解决问题.

我试过补充一下

- (void)dealloc {
    NSLog(@"dealloc");
}
Run Code Online (Sandbox Code Playgroud)

当细胞将被解除分配但我从未得到任何东西时捕获.

编辑-(void)dealloc在子类UITableViewCell中有我的方法.

编辑 这是我创建单元格的地方/方式

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
 static NSString* inventoryCellID = @"InventoryCustomCellID";

 InventoryCustomCell* cell = (InventoryCustomCell *)[tableView dequeueReusableCellWithIdentifier:inventoryCellID forIndexPath:indexPath];

 [self configureCell:cell atIndexPath:indexPath];

 return cell;
 }


- (void)configureCell:(InventoryCustomCell *)cell atIndexPath:(NSIndexPath *)indexPath {

    [cell formatCellWithProduct:[fetchedResultsController objectAtIndexPath:indexPath] enableAdding:NO];

    cell.openThumbnailButton.tag = indexPath.row;

    [cell.openThumbnailButton addTarget:self action:@selector(presentThumbnailViewWithCell:) forControlEvents:UIControlEventTouchUpInside];
}
Run Code Online (Sandbox Code Playgroud)

在我的自定义单元格中,这是被调用的配置方法:

- (void)formatCellWithProduct:(Product*)product enableAdding:(bool)addingEnabled {

    self.titleLabel.text = product.part_number;
    self.partNumberLabel.text = [[[product.manufacturer allObjects] objectAtIndex:0] name];

    //The SDWebImage UIImageView category method
    [self.thumbImageView setImageWithURL:[NSURL URLWithString:product.photo] placeholderImage:[UIImage imageNamed:@"icon.png"]];
}
Run Code Online (Sandbox Code Playgroud)

编辑 这是下载图像并设置它的SDWebImage方法.

- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletedBlock)completedBlock;
{
    [self cancelCurrentImageLoad];

    self.image = placeholder;

    if (url)
    {
        __weak UIImageView *wself = self;
        id<SDWebImageOperation> operation = [SDWebImageManager.sharedManager downloadWithURL:url options:options progress:progressBlock completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished)
        {
            __strong UIImageView *sself = wself;
            if (!sself) return;
            if (image)
            {
                sself.image = image;
                [sself setNeedsLayout];
            }
            if (completedBlock && finished)
            {
                completedBlock(image, error, cacheType);
            }
        }];
        objc_setAssociatedObject(self, &operationKey, operation, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
}
Run Code Online (Sandbox Code Playgroud)

Sea*_*ell 15

表视图不倾向于分配和释放表视图单元.创建单元格很昂贵,因此可以在可能的情况下重复使用,而不是在屏幕外时丢弃.

UITableViewDelegate方法-tableView:didEndDisplayingCell:forRowAtIndexPath:是更新单元格以取消下载或其他不再相关操作的更好位置.

但是,看起来每次调用-setImageWithURL:etc:etc:都试图取消该图像视图的先前下载.