SRMR
SRMR

Reputation: 3144

Table View Cells "jump" when going back from View Controller to Table View Controller

I have a TableViewController, and clicking on a custom cell takes you to a related WebViewController.

The problem I'm having is that when I tap "Back" in the WebViewController the Table View Cells in TableViewController "jump".

enter image description here

Once the Table View starts scrolling they all jump back into the right heights. So I'm assuming it has something to do with heights on the TableViewController custom cells, but I'm not totally sure.

TableViewController.m

Tried:

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    id model = self.Model[indexPath.row];

    if ([model isKindOfClass:[Two self]]) {
        return 490; // As of 11/13/14
    } else { // 2 other custom cells
        return tableView.rowHeight; // return the default height
    }
}

Also Tried:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    id model = self.Model[indexPath.row];

    if ([model isKindOfClass:[One self]]) {
    ListTableViewCell *cellOne = [tableView dequeueReusableCellWithIdentifier:@"1Cell"];
    CGFloat heightOne = [cellOne.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
    return heightOne + 2;

    } else if ([model isKindOfClass:[Two self]]) {
    ListTableViewCellTwo *cellTwo = [tableView dequeueReusableCellWithIdentifier:@"2Cell"];
    CGFloat heightTwo = [cellTwo.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
    return heightTwo + 400;

    } else if ([model isKindOfClass:[Three self]]) {
    ListTableViewCellThree *cellThree = [tableView dequeueReusableCellWithIdentifier:@"3Cell"];
    CGFloat heightThree = [cellThree.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
    return heightThree + 2;

    } else {
        return 300;
    }
}

UPDATE:

Also tried [cell setNeedsUpdateConstraints]; and [cell setNeedsLayout];.

Also have tried setting estimatedHeight multiple different spots, and also added Automatic Row Dimension in tandem with that.

Help would be appreciated! Will post any extra info needed. Thanks!

I'm only supporting iOS 8. I'm using Auto Layout and Storyboards.

Additional Storyboard info for Custom Cell 2: enter image description here enter image description here

Storyboard Constraints:

enter image description here

Upvotes: 4

Views: 1258

Answers (3)

Thoast83
Thoast83

Reputation: 529

I finally solved this issue, the key is to calculate an estimated height. Seems like the more accurate estimate you return, the less jumpiness/choppiness.

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
     switch (yourTypeOfCell) {
         case something:
            return estimatedHeight;
            break;
         case default:
            return defaultValue;
            break;
     }
}

Upvotes: 1

hasan
hasan

Reputation: 24195

I believe that you didn't set the constraint properly. I had the a problem similar to this before and solved by setting the constraints on Leading, Trailing, Top and Bottom spacing to the UILabel.

Play around with the constraints. behavior will start change. keep going until you get what you want.

It doesn't seem as a decesnt technical answer from a professional. But, that how it getting solved with those dummy constraint which apple doesn't introduced properly.

Also, try [cell setNeedsUpdateConstraints]; before you return cell in cellForRowAtIndexPath. And: [cell setNeedsLayout];

Upvotes: 2

Andy Obusek
Andy Obusek

Reputation: 12842

Try setting the estimatedRowHeight in viewDidLoad for your UITableView to help UIKit guess your tableView's row height

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.estimatedRowHeight = 490.0;
}

Upvotes: 0

Related Questions