7

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.

Thilina Chamath Hewagama
  • 9,039
  • 3
  • 32
  • 45

8 Answers8

9

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!

chasew
  • 8,438
  • 7
  • 41
  • 48
5

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

enter image description here

StrawHara
  • 1,301
  • 19
  • 33
3

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)
Rikh
  • 4,078
  • 3
  • 15
  • 35
  • I want to show a different text when the user clicks on the cell. So reloading the table doesn't solve my problem. – Thilina Chamath Hewagama Dec 27 '16 at 11:38
  • Whenever you update a `UITableView` you need to keep track of any changes you make using a model owing to the re-usability of cells. After you change the text, if you scroll the screen your changes will be lost. Example: In your `cellForRow` you must have initially set the label to be something, if you change it and scroll again without updating a model, the value will be lost. – Rikh Dec 27 '16 at 11:41
3

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.

2

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()
    }
}
Thiha Aung
  • 5,036
  • 8
  • 36
  • 79
0

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.

Aman Gupta
  • 985
  • 1
  • 8
  • 18
0

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

}
Morshed Alam
  • 153
  • 2
  • 10
-2

Add the following in your viewDidLoad()

-(void)viewDidLoad{
   [super viewDidLoad];
   tableView.rowHeight = UITableViewAutomaticDimension
   tableView.estimatedRowHeight = 140
}
Vineesh TP
  • 7,755
  • 12
  • 66
  • 130