Search code examples
ioscore-datansfetchedresultscontroller

_fetchedResultsController objectAtIndexPath:indexPath freezes app


It takes too much time on [_fetchedResultsController objectAtIndexPath:indexPath], is there any alternative way ? heightForRowAtIndexPath takes too much time on this point. I don't know this is wrong question or not, but please help me to get the proper solution.

   - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath   *)indexPath
 {
    ThreadInfo *info=[_fetchedResultsController objectAtIndexPath:indexPath];
    NSString* objectId = info.threadID;
    if (![self.idToCellHeight objectForKey: objectId])
    {
        CGFloat height = [self calculateHeightForId:info];
        [self.idToCellHeight setObject:[NSNumber numberWithFloat:height] forKey: objectId];
    }
    return [[self.idToCellHeight objectForKey:objectId] floatValue];

}

Solution

  • The alternative way is to use self-sizing cells. You won't have to calculate (or cache) any row heights.

    1. Add Auto Layout constraints to your tableViewCell's subviews which will cause the cell to adjust its height based on the subview's contents.
    2. Enable row height estimation.

      self.tableView.rowHeight = UITableViewAutomaticDimension; self.tableView.estimatedRowHeight = 44.0;

    For more information, see the detailed walkthrough by smileyborg in his answer to Using Auto Layout in UITableView for dynamic cell layouts & variable row heights.