Dynamically increase height of UILabel & TableView Cell?

Viewed 33233

I have a UITableView in which i am displaying a custom cell.I my cell i have two label & one view as below in picture.

enter image description here

I have given constraint of left view like this

Item label constraints

enter image description here

center view constraints

enter image description here

right view constarints

enter image description here
I am using a bean class to store data for two labels & add that bean object into one array.I am using below code in heightForRowAtIndexPath.

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    // Calculate a height based on a cell
    if(!self.customCell) {
        self.customCell = [self.tableView dequeueReusableCellWithIdentifier:@"thirdcell"];
    }

    // Configure the cell
    Instrument *inst=[arr_instSpecs objectAtIndex:indexPath.row];
    self.customCell.label_item.text=inst.item;
    self.customCell.label_desc.text=inst.desc;


    // Layout the cell

    [self.customCell layoutIfNeeded];

    // Get the height for the cell

    CGFloat height = [self.customCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;

    // Padding of 1 point (cell separator)
    CGFloat separatorHeight = 1;

    return height + separatorHeight;
}

Problem is neither height of label is increasing nor of table view cell.I have explained everything. I want to make size of label increase when there is increase in label's text & also when label size increase then height of cell must increase.

8 Answers

This is how I worked out.

1) Set no. of lines to 0.

2) Set LineBreakage.

enter image description here

3) Add Constraints.

enter image description here

4) Change vertical content hugging priority.

enter image description here

5) On ViewDidLoad:

override func viewDidLoad() {
    super.viewDidLoad()

    self.sampleTableView.estimatedRowHeight = 600.0;
    self.sampleTableView.rowHeight = UITableViewAutomaticDimension;

    self.sampleTableView.setNeedsLayout()
    self.sampleTableView.layoutIfNeeded()
}

6) On TableView Custom Cell:

override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code

    sizeToFit()
    layoutIfNeeded()
}

Swift 3 / Swift 4

Custom cell:

class CustomCell: UITableViewCell {

    @IBOutlet var messageLabel: UILabel!

    override func awakeFromNib() {
        super.awakeFromNib()

        sizeToFit()
        layoutIfNeeded()

    }

}

viewDidLoad:

override func viewDidLoad() {
    super.viewDidLoad()        

    self.tableView.estimatedRowHeight = 80
    self.tableView.rowHeight = UITableViewAutomaticDimension

}

And cellForRowAtIndexPath:

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

     let cell : CustomCell = tableView.dequeueReusableCell(withIdentifier: "yourcellIdentifier", for: indexPath) as! CustomCell
     cell.messageLabel.text = exampleContent[indexPath.row]
     cell.messageLabel.sizeToFit()
     cell.messageLabel.layoutIfNeeded()
     return cell

}

Swift 4.2

For the Basic or Subtitle type of cell, first in viewDidLoad you do this:

    self.tableView.estimatedRowHeight = 44 // This is the default cell height
    self.tableView.rowHeight = UITableView.automaticDimension

enter image description here

Then select the Title and set its number of lines to 0. Now the row will adjust its height based on the Title text. You don't need to do anything else.

enter image description here

Same goes for your Custom cell types. Make sure the Label which you use in your custom cell is set for 0 lines as well.

I was working on someone else's code and none of the given answers worked for me. The constraints were correct. Changing the "Desired Width" of label to automatic, in the storyboard did the job for me me. To do this, uncheck the "Explicit" checkmark in the Size Inspector for the label.

It should be like this

Related