2

I created a dynamic cell using AutoLayout, put code in (void)updateConstraints cell's subview method, set BOOL value so custom AutoLayout code runs only once, and in View Controller call

  [cell setNeedsUpdateConstraints];
  [cell updateConstraintsIfNeeded];

just before returning cell. Everything seems to works fine but I'm experiencing weird issue, when I select cell all its subviews (or itself?) change position.

table view

cell pointed with this beautiful arrow shows it ;]

rounak
  • 9,217
  • 3
  • 42
  • 59
Michal Gumny
  • 1,770
  • 1
  • 16
  • 24
  • When the cell is selected, what changes are expected? – Lyndsey Scott Dec 03 '14 at 15:46
  • I added tap handlers for user image and link url, cell shoudn't be selected and I even disabled it - added [super setSelected:NO animated:NO]; inside appropriate method in cell's subclass – Michal Gumny Dec 03 '14 at 15:52
  • FYI This line `[super setSelected:NO animated:NO];` would not disable the ability to select the cell after it's created. – Lyndsey Scott Dec 03 '14 at 15:56
  • P.S. I'm not implying the cell should shift just because it's selected... it shouldn't. But you should look into the rest of your code while keeping the fact that the row is in fact "selected" in mind. For example, perhaps the shift is happening in one of the relevant delegate methods. – Lyndsey Scott Dec 03 '14 at 16:04
  • actually it was good point, I have set cell selection style to UITableViewCellSelectionStyleNone, and now see that sometimes cell display subviews in a little different positions even if they have exactly this same data used (same image, text etc.) – Michal Gumny Dec 03 '14 at 16:38
  • OK, I think I see what's happening. I'll write out a suggestion now. – Lyndsey Scott Dec 03 '14 at 16:41

2 Answers2

1

I experienced the same bug and tinkered for about a day to fix this ... to get to the bottom of the issue I set different background colors on the cell & the content view. Right after first showing the table view everything seemed to work, but after selecting the cell the contentView would jump around - sometimes when selecting, sometimes when deselecting (which I do immediately in tableView:didSelectRowAtIndexPath:), and the cell's background became visible. So I figured the cell somehow didn't reestablish the correct contentView dimensions.

Finally it turned out that at least in iOS 8 you need to set the appropriate autoresizingMasks for the cell as well as the contentView, and then make the layout system translate them into constraints by setting translatesAutoresizingMaskIntoConstraints to YES. At the same time this flag needs to be NO on all subviews of your contentView.

So here's my initialisation for custom cells:

- (void)awakeFromNib {

    // enable auto-resizing contentView
    [self setTranslatesAutoresizingMaskIntoConstraints:YES];
    [self.contentView setTranslatesAutoresizingMaskIntoConstraints:YES];
    self.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    self.contentView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;

    // remove autoresizing constraints from subviews
    for (UIView *view in [self.contentView subviews]) {
        view.translatesAutoresizingMaskIntoConstraints = NO;
    }
}

Works like a charm for me. For the record, I'm using the UITableViewCell auto-sizing which was introduced in iOS 8, like this:

// enable automatic row heights in your UITableViewController subclass
self.tableView.rowHeight = UITableViewAutomaticDimension;
self.tableView.estimatedRowHeight = 80.0; // set to whatever your "average" cell height is
if0xx
  • 21
  • 2
0

I found solution to this issue,I used instructions under this link dynamic-cell-layouts-variable-row-heights to implement dynamic table view cells: [Using Auto Layout in UITableView for dynamic cell layouts & variable row heights

[1]: Using Auto Layout in UITableView for dynamic cell layouts & variable row heights. Author suggests to use this two methods to update constrains when using updateConstraints method inside cell's subview.

[cell setNeedsUpdateConstraints];
[cell updateConstraintsIfNeeded];

but what I found in my case is there was some issue that not all cells was updated as they should. Solution is to call [cell updateConstraints] in (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath and not wait until system will update constrains.

Community
  • 1
  • 1
Michal Gumny
  • 1,770
  • 1
  • 16
  • 24