Ranjit
Ranjit

Reputation: 4636

UITableView bounces with estimatedRowHeight

I am using autoLayout for my customCell in xib, because I want to have variable height for rows based on the text.

Now in my VC.m

I am using these lines of code

- (void)viewdidLoad
{
    m_tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];       
    m_tableView.estimatedRowHeight = 97.0;
   m_tableView.rowHeight = UITableViewAutomaticDimension;
}

here is my cellForRow function

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    CustomCell *tableCell = (NotesCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (tableCell == nil) {

        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:self options:nil];
        tableCell = [nib objectAtIndex:0];
    }    
    // Configure the cell...


     //This code is used to make the separator full width. Also the tableview separator insets should be set to zero

    tableCell.layoutMargins = UIEdgeInsetsZero;
    tableCell.preservesSuperviewLayoutMargins = NO;

    return tableCell;
}

So now whenever I update a row I call these lines of code

[m_tableView beginUpdates];
    [m_tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:m_indexPath] withRowAnimation:UITableViewRowAnimationFade];
    [m_tableView endUpdates];

When I do this, by Tableview just bounces. I dont know what is going wrong

Regards Ranjit

Upvotes: 4

Views: 972

Answers (2)

dan
dan

Reputation: 9825

This is happening because when you reload your cell the table view is asking for the heights of all the cells again. Any cells that are offscreen above your scroll position will be asked for their estimated row heights and if they are different from their current height it will cause the cells to change height and you will see the table bounce like that.

Once your cells have their heights calculated you should cache that value somewhere and then implement tableView:estimatedHeightForRowAtIndexPath: and return the cells actual height if they have one instead of your estimated height.

Upvotes: 1

Rory McKinnel
Rory McKinnel

Reputation: 8014

You should use dequeueReusableCellWithIdentifier:forIndexPath rather than the version you are using.

The one you are using will have no size class due to the fact the cell returned has no parent. Its an issue which appears from xcode 6 onwards due to the size class changes.

Try changing to this version which returns a cell which is part of the table and thus inherits the traitCollection of the superviews and helps the layout to work properly.

Upvotes: 0

Related Questions