1

Autolayout of my UITableViewCell works as expected when viewing, but when I selecting a row, the UI will break, and the height of this cell will also back to default value.

For cell-related code, I just followed this tutorial "Using Auto Layout in UITableView for dynamic cell layouts & variable row heights"

Unfortunately, this tutorial doesn't cover selection part of UITableViewCell

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewAutomaticDimension;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    cell = ...
    [cell setNeedsUpdateConstraints];
    [cell updateConstraintsIfNeeded];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.navigationController pushViewController:MyViewController animated:YES];
}
Community
  • 1
  • 1
Yuwen Yan
  • 4,777
  • 10
  • 33
  • 63
  • Are you adding the constraints in the storyboard? could you add more details about them? Also, you should not need to call `[cell setNeedsUpdateConstraints]; [cell updateConstraintsIfNeeded];` – Catalina T. Mar 19 '15 at 12:17
  • If you are targeting iOS8, you don't need `heightForRowAtIndexPath`at all. If you are targeting iOS7, don't return `UITableViewAutomaticDimension`, but look in the page you linked to how to set up an offscreen cell.. – koen Mar 19 '15 at 15:19

0 Answers0