Question

I'm new to iOS development, in my application I need to calculate the height of dynamic table cell in heightForRowAtIndexPath method.

The problem is that I don't want to return value before "callback" block is finished.. is there a way in objective-c to do this?

That's my method implementation:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    ...
    SDImageCache *imageCache = [[SDImageCache alloc] init];
    [imageCache queryDiskCacheForKey:model.thumbnail done:^(UIImage *image, SDImageCacheType cacheType)
     {

         // do calculations with height etc.
         // got an error "incompatible block pointer types sending"
         return value;
     }];
}
Was it helpful?

Solution

You will either have to run your block synchronously (that is, block the main thread until the block's completion) and return it then, or you will need to store the calculated height in a storage and reload the cell. The second option is preferred because you are not blocking the main thread and UI. You could present a loading indicator on a placeholder cell, and once you have all the required information, reload the cell and present it properly. You can specify an animation to make this reload transition smooth for the user.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top