UITableView self sizing cell scrolling issues with big difference of cell height

630 Views Asked by At
  func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{

    let json = JSON(data: activityTableView.onlineFeedsData)[indexPath.row] // new
    if(json["topic"]["reply_count"].int > 0){
        if let cell: FeedQuestionAnswerTableViewCell = tableView.dequeueReusableCellWithIdentifier(kCellIdentifier_reply) as? FeedQuestionAnswerTableViewCell{

            cell.selectionStyle = .None
            cell.tag = indexPath.row
            cell.relatedTableView = self.activityTableView
            cell.configureFeedWithReplyCell(cell, indexPath: indexPath, rect: view.frame,key: "activityTableView")
            // Make sure the constraints have been added to this cell, since it may have just been created from scratch
            cell.layer.shouldRasterize = true
            cell.layer.rasterizationScale = UIScreen.mainScreen().scale

            return cell
        }
    }else{
        if let cell: FeedQuestionTableViewCell = tableView.dequeueReusableCellWithIdentifier(kCellIdentifier) as? FeedQuestionTableViewCell{

            cell.selectionStyle = .None
            cell.tag = indexPath.row
            cell.relatedTableView = self.activityTableView
            cell.configureFeedCell(cell, indexPath: indexPath, rect: view.frame)
            // Make sure the constraints have been added to this cell, since it may have just been created from scratch

            cell.layer.shouldRasterize = true
            cell.layer.rasterizationScale = UIScreen.mainScreen().scale
            return cell
        }
    }


    return UITableViewCell()
}

There is difference of 200-400 in height in each cell,So tried implementing the height calculation in estimatedHeightForRowAtIndexPath .I cant the exact estimate height in this method as bcz of calculation

and caching the height in willDisplayCell method but still the scroll jumps like is it taking time to rendering .

The cell is like the Facebook card type layout with lots of dynamic data with variable height text and images.Can someone help me in this .Thanks

2

There are 2 best solutions below

1
On

Try this one. I hope this could help you. It's worked for me. Actually this calculates the cell height before it displayed. Thanks.

NSMutableDictionary *cellHeightsDictionary;

// Put this in viewDidLoad

cellHeightsDictionary = @{}.mutableCopy;

// UITableview delegate Methods

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
NS_AVAILABLE_IOS(7_0)
{
    NSNumber *height = [cellHeightsDictionary objectForKey:indexPath];
    if (height) return height.doubleValue;
    return UITableViewAutomaticDimension;
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    [cellHeightsDictionary setObject:@(cell.frame.size.height) forKey:indexPath];

}
1
On

If you're able to calculate each cells height, just use the tableView(_:heightForRowAtIndexPath) instead of estimatedRowHeightAtIndexPath property. estimatedRowHeightAtIndexPath is mostly used in smthg like selfsizing cells etc.