indexPathForCell获取indexPath为nil的情况

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/PianZhideNanRen/article/details/79958126

有时候我会利用indexPathForCell方法获取指定cell的indexPath,我的项目的有一个使用场景:在cell里有个图片,在利用sdImage下载完成图片后要回调到controller设置源数据指定Model的Image,这个图片可能还要其他操作,所以要保存起来。但是在根据cell获取IndexPath的时候楚翔了问题,获取的都是nil;

下面上片段代码:

GoodsSkuTableViewCell.m
[self.skuShowImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", model.imagePath, Thumbnail_image_Suffix]] placeholderImage:[UIImage imageNamed:@"goods_add_type_image"] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
                    
                    //下载完成后回调
                    if (image && weakSelf.delegate && [weakSelf.delegate respondsToSelector:@selector(tableViewCell:TypeImageDownloadFinish:)]) {
                        //这里是解决方案
                        model.image = image;
                    }
                }];
AddGoodsView.m
- (void)tableViewCell:(GoodsSkuTableViewCell *)cell TypeImageDownloadFinish:(UIImage *)image {
    //这里有可能是nil
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
}

为什么获取的是nil呐;

后来经过测试只有在tableVew刷新完成创建好当前显示的cell才能调用

[self.tableView indexPathForCell:cell]
否则是无效的

猜你喜欢

转载自blog.csdn.net/PianZhideNanRen/article/details/79958126