2

I am doing UITableview cell height (Dynamic height) as per UIImageView height, currently it's working fine but my problem is UITableview is not smoothly scrolling or not working properly if i scroll fastly

I think problem is i am reloading the row in tableview cell so tableview scroll is not working can anyone tell me any good suggestion for this problem ?

Here what i did

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

// Create the cell...

[cell.myImageView setImageWithURL:[NSURL URLWithString:@"http://example.com/image.jpg"]
          placeholderImage:[UIImage imageNamed:@"placeholder"]
                 completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
    if (image != nil) {
        [self.images insertObject:image atIndex:indexPath.row];
        [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:YES];
    }
}];
return cell;
}

For setting height as per Image

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
id image = [self.images objectAtIndex:indexPath.row];
if ([image isKindOfClass:[NSNull class]]) {
    return defaultHeight;
} else {
    return [image size].height + topPadding + bottomPadding;
}
}
Mayank Patel
  • 3,868
  • 10
  • 36
  • 59
  • Why are you reloading your UITableViewCell? Also, are you using some sort of networking library like AFNetworking? – Kunal Shah Feb 23 '17 at 10:39
  • Yes i am usin AFNetworking and SDWebimage i am reloading row because i need to set uitableview cell height as per UIIMageview height @KunalKShah – Mayank Patel Feb 23 '17 at 10:40
  • @MayankPatel I recently checked you are using third party which managed to download in async. – dahiya_boy Feb 23 '17 at 10:44
  • @agent_stack i don't have problem with image but i have cell height issue – Mayank Patel Feb 23 '17 at 10:46
  • For dynamic height for table use `uitableviewautomaticdimension` in `heightForRowAtIndexPath ` and also write `estimatedHeightForRowAtIndexPath ` with some expected height. – dahiya_boy Feb 23 '17 at 10:47
  • ok thanks let me try with this – Mayank Patel Feb 23 '17 at 10:48
  • It makes more sense to pre-download your images than to calculate row heights on the fly as this may affect your scrolling performance too. – Kunal Shah Feb 23 '17 at 10:49
  • @MayankPatel In your question you wrote `problem is UITableview is not smoothly scrolling` so I thought your UI got stucked. And you didn't mentained any where that you have dynamic height issue. Please write clearlt. – dahiya_boy Feb 23 '17 at 10:49
  • @agent_stack edited :) – Mayank Patel Feb 23 '17 at 10:50
  • @KunalKShah pre download image is not a good idea why should i download all the images – Mayank Patel Feb 23 '17 at 10:51
  • @MayankPatel create a function which downloads images in `asyn mode` and add each image in array if any image is downloaded. Put condition if array value is change then reload data. Hope it helps. – dahiya_boy Feb 23 '17 at 10:54
  • And never reload tableView and collectionView in its cell. It is never be a good approach. – dahiya_boy Feb 23 '17 at 10:56
  • i think we need to reload then how we know what is height of imageview so we need to put the temp height as beggining – Mayank Patel Feb 23 '17 at 10:57
  • Since you are using _SDWebImage_ to download images. You must know that it downloads images asynchronously and it also caches them to the disk (not memory). So pre-downloading images is a good idea in your case as @KunalKShah mentioned. However, adding images to an array would probably be the worst approach as it'll cause memory issues and may cause your application to crash. – Adeel Miraj Feb 23 '17 at 11:12
  • then how can i fix this problem or what is the other way i can set cell height as per image ? – Mayank Patel Feb 23 '17 at 11:16

1 Answers1

-2

try this

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewAutomaticDimension;

}

and

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
            UIImage *image = [self.images objectAtIndex: indexPath.row];
            if (image) {
                return image.size.height + 5;
            }
            else {
                return 80.0; //default value......
            }
        }