LuRui
LuRui

Reputation: 111

How to change UITableViewCell's height dynamically using Auto Layout?

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~~~

Upvotes: 4

Views: 3070

Answers (5)

noveleven
noveleven

Reputation: 637

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];

Upvotes: 1

tehR
tehR

Reputation: 11

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];
}

Upvotes: 0

Priyal
Priyal

Reputation: 877

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;
}

Upvotes: 2

Devika S
Devika S

Reputation: 42

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

self.tableView.rowHeight = UITableViewAutomaticDimension;

Upvotes: 1

Dhaval Bhadania
Dhaval Bhadania

Reputation: 3089

  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)

Upvotes: 0

Related Questions