0

I should load an image from an URL and set the height of row in tableView with the height of image. To do so, I use the category methods of SDWebImage in cellForRowAtIndexPath:

NSString *imageURL = [_storeCommodityDetailModel.detailImageURLs objectAtIndex:row];

__weak typeof(UIImageView) *weakImageView = imgViewCell.commodityImageView;
__weak typeof(UIImageView) *weakHoldplaceImageview = imgViewCell.placeholdImageView;
[imgViewCell.placeholdImageView setImage:[UIImage imageFromFile:DEFAULT_IMAGE_REPLACE]];
[imgViewCell.commodityImageView setImageWithURL:[NSURL URLWithString:imageURL] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
    weakImageView.image = image;
    CGFloat section = ((NSIndexPath *)_detailImageHeights[row]).section;
    CGFloat height = ((NSIndexPath *)_detailImageHeights[row]).row;
    if (section == 0) {
        [weakHoldplaceImageview removeFromSuperview];
        if (0 != image.size.width) {

            if (image.size.height < 300) {
                height = 0;
            }else{
                height = weakImageView.width*image.size.height/image.size.width;
            }
            NSIndexPath *index = [NSIndexPath indexPathForRow:height inSection:1];
            [_detailImageHeights replaceObjectAtIndex:row withObject:index];

            [_tableView beginUpdates];
            [_tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
            [_tableView endUpdates];;
        }
    }
    weakImageView.height = height;
}];

The problem is that it always crashes on the following line:

[_tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone]

and I continually get the error in the error logs of the following:

Invalid update: invalid number of rows in section 2. The number of rows contained in an existing section after the update (0) must be equal to the number of rows contained in that section before the update (3), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'


I found the Reason,you can see the question reloadRowsAtIndexPaths:withRowAnimation: crashes my app, Nekto say that when you are trying to reload some cell table check the size of table view and sees that it has been changed,That results in a crash

Community
  • 1
  • 1
morisunshine
  • 780
  • 4
  • 7

1 Answers1

0

You are updating second section of your tableview, Put 0 in section

NSIndexPath *index = [NSIndexPath indexPathForRow:height inSection:0];
Toseef Khilji
  • 17,192
  • 12
  • 80
  • 121