Thilina Chamath Hewagama
Thilina Chamath Hewagama

Reputation: 9040

Dynamic UITableViewCell not resizing according to the content

enter image description here

Senario A: If I set the label content in cellForRowAtIndexPath, the cell correctly get resized.

Senario B: If I change the text content in custom action in cell, the cell sized does not get changed.(I do call setNeedsLayout + layoutIfNeeded)

How to fix this?

EDIT:

1) I have set, myTableView.estimatedRowHeight = 71.0 myTableView.rowHeight = UITableViewAutomaticDimension

2) I have correctly added auto layout constraints.

Upvotes: 7

Views: 12388

Answers (8)

StrawHara
StrawHara

Reputation: 1321

In my case, the cell's custom size was enabled:

enter image description here

Upvotes: 5

Federico Arvat
Federico Arvat

Reputation: 79

In my case, in the same cell I had an imageView in the top left corner with a "center vertically in container" constraint, and a "top space container" constraint.

Obviously to satisfy this two constraint the cell must have an height equal to:

(height of the imageView / 2) + (length of the top space container constraint).

This height is not enough to fit the label text, so my label had only 1 line visible.

After I have deleted the imageView top constraint all went to the right place, in my case i wanted the image to be centered, if the image had to stay in the top left corner I had to take off the "center vertically in container" constraint.

I hope this can help someone.

Upvotes: 3

chasew
chasew

Reputation: 8828

I was running into this issue, and my problem was that I was constraining the content to self (the UITableViewCell) and not to self.contentView (the contentView OF the cell). Hope this helps someone else who has built their cells all in code!

Upvotes: 9

Thiha Aung
Thiha Aung

Reputation: 5096

First of all, I don't specifically know what was your action on UITableViewCell. So, I assume I do that in UITableViewCell selection.

The below answer only work on iOS 9 and above

But, for some reason, it failed to do it in iOS 8 until it scroll. So, I will update the answer for iOS 8.

I have seen you have used UITableView's estimatedRowHeight and rowHeight at your project. So,

Please check the following

  1. Make sure UITableView's estimatedRowHeight and rowHeight include inside viewDidLoad()
  2. Make sure your UILabel lines set to 0

enter image description here

  1. Make sure there is no constraints about height for your UILabel and let the constraints be like that :

enter image description here

If there are other component also included, make sure only bottom and top space constraints included or top space to container margin and bottom space to container margin.

  1. Every time that you want to update the cell, you have to reload tableView no matter what your current situation will be.

So, don't say anything yet before you try this sample project, @Rikh answer still work. May be you are going in wrong direction. Here's the solution. Please do as I said steps by steps and let me know if that didn't work out. You might need to share your sample project which is causing.

enter image description here

Sample Demo - DynamicCellDemo

UPDATE for iOS 8 : update the following code for iOS 8 users

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    if #available(iOS 9, *) {
        // do nothing
    } else {
        tblDynamic.reloadData()
    }
}

Upvotes: 2

Morshed Alam
Morshed Alam

Reputation: 153

Try this if it works:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

     return UITableViewAutomaticDimension

}

 func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {

    return UITableViewAutomaticDimension

}

Upvotes: 0

Rikh
Rikh

Reputation: 4222

After you change the text of the cell, just reload that particular cell or simply call mainTableView.reloadData().

To reload that cell-

//indexPath is indexPath of cell you just changed label of
mainTableView.reloadRows(at: indexPath, with: .automatic)

Upvotes: 3

Aman Gupta
Aman Gupta

Reputation: 985

what you can do is set the AutoLayout constraints for the label in present in the cell from all the sides that is from Top, Bottom, Leading and Trailing. Then add the following UITableViewDelegate method to your class.

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
    return 500
}

This will do the job. As now content in table view cell automatically adjusts the height of the cell.

Upvotes: 0

Vineesh TP
Vineesh TP

Reputation: 7963

Add the following in your viewDidLoad()

-(void)viewDidLoad{
   [super viewDidLoad];
   tableView.rowHeight = UITableViewAutomaticDimension
   tableView.estimatedRowHeight = 140
}

Upvotes: -2

Related Questions