4

A self-sizing UITableViewCell: MyTableViewCell.m

-(void) viewDidLoad{
        UIView *sample = [[UIView alloc] init];
        [self.contentView addSubview: sample];

        //Fake code here. Let sample view fill the whole contentView:
        Constraint1: sample.leading = contentView.leading;
        Constraint2: sample.trailing = contentView.trailing;
        Constraint3: sample.top = contentView.top;
        Constraint4: sample.bottom = contentView.bottom;
        Constraint5: sample.height = 20;
        NSLayoutConstraint:activateConstriant(constraints 1-5);
}

This code works and the MyTableViewCell has a height of 20.

However, I want to change the height at run time (after this cell added to a UITableView), so I add a new method to MyTableViewCell.m

-(void) updateHeight:(CGFloat)newHeight{
        //Fake code here. change the constant of constraint1 
        Constraint1 = get reference of Constraint1 in viewDidLoad();
        Constraint1.constant = newHeight
}

I run the code and call updateHeight:10 and got the log warning:

2017-03-16 16:23:11.102858 [14541:568021] [LayoutConstraints] Unable to simultaneously satisfy constraints. Probably at least one of the constraints in the following list is one you don't want. Try this: (1) look at each constraint and try to figure out which you don't expect; (2) find the code that added the unwanted constraint or constraints and fix it. ( "", "", "", "" ) Will attempt to recover by breaking constraint

My own opinion is that my constraints created at viewDidLoad automatically creates couple of other constraints like contentView.height=20 and my change to the original constraints conflict with the constraints created by system.

I tested to change the leading of sample view instead of the height in updateHeight:, and it works! Because the change of leading doesn't affect contentView's own height constraint?

How am I supposed to do a dynamically change to auto layout like this case?

Thank you~~~

nynohu
  • 1,628
  • 12
  • 12
LuRui
  • 111
  • 2
  • 4
  • Try `sample.translatesAutoresizingMaskIntoConstraints = NO`. More often than not, this solves the problem. – Rikh Mar 16 '17 at 09:02
  • 2
    Did you find out eventually? All answers here are for initialisation, not updating it on the go which is what you (and I) need. Any findings? – Lapidus Jun 26 '17 at 10:33

5 Answers5

2

To have dynamic cell size you need to implement the below methods

-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return <your estimated cell height>;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewAutomaticDimension;
}
Priyal
  • 879
  • 1
  • 9
  • 30
  • I've already make this a self-sizing cell. My problem is to change the height at run time, but thanks anyway – LuRui Mar 16 '17 at 09:31
  • @LuRui can be explain on what exactly is the problem ? Are broken constraints your concern ? – Priyal Mar 16 '17 at 09:51
1

Set the tableview row height in your view controller. Refer the following code line :

self.tableView.rowHeight = UITableViewAutomaticDimension;
Nilesh
  • 701
  • 5
  • 14
Devika S
  • 42
  • 6
  • I've already make this a self-sizing cell. My problem is to change the height at run time, but thanks anyway~. – LuRui Mar 16 '17 at 09:31
1

Make sure your updateHeight: method executed before cell's layoutSubviews. Because if cells has been loaded, you have to reload the tableView to make your new constraints effective, so you can figure out your automatic constraints in tableView:cellForRowAtIndexPath: method, or the following code is what you need.

[tableView beginUpdates];
// your code, alter your cell's constraints here
[tableView endUpdates];
noveleven
  • 569
  • 7
  • 17
0
  tableView.estimatedRowHeight = 44.0 //put as per your cell row  height
  tableView.rowHeight = UITableViewAutomaticDimension
  • Give Top,Bottom,Leading,Trailing Contraints to UILabel used.

  • Be sure numberOfLine for your UILabel in the cell is set to 0.

  • Estimated row height needs to be provided.(Provide any appropriate value)

Dhaval Bhadania
  • 3,090
  • 1
  • 20
  • 35
  • I've already make this a self-sizing cell. My problem is to change the height at run time, but thanks anyway – LuRui Mar 16 '17 at 09:31
  • @LuRui what u want exactly ??if u want to run time give data to cell and after self.tableView.reloadData() will manage all height automatically .may it will help – Dhaval Bhadania Mar 16 '17 at 09:34
0

I ran into this issue today and solved it by overriding the UITableViewCell object's drawRect method.

- (void) drawRect:(CGRect)rect {
    [super drawRect:rect];

    [self adjustConstraint];
}
tehR
  • 11
  • 1