7

I was following this tutorial to make self sizing cells.

I registered custom cell into table view and in the cell xib I gave every subview constraints manually. Please refer to the source code in GitHub

Arranging 3 labels vertically in one cell's content view worked fine in iOS 9 but not in iOS 8 (both tested in device and simulator).

In iOS 8 one cell does not have fitting height and not every label shows all it's full text.

iOS 8 not fitting:

enter image description here

iOS 9 as I expected:

enter image description here

Any advice would be appreciated!

Update

code in viewDidLoad:

 tableView.registerNib(UINib.init(nibName: "SelfSizingCell", bundle: nil), forCellReuseIdentifier: cellIdentifier)
 tableView.dataSource = self
 tableView.delegate = self
 tableView.estimatedRowHeight = 60.0
 tableView.rowHeight = UITableViewAutomaticDimension
 tableView.reloadData()
bluenowhere
  • 2,683
  • 5
  • 24
  • 37
  • Did you set `tableView.estimatedRowHeight` in `viewDidLoad`? – rob mayoff Jan 04 '16 at 03:48
  • @robmayoff yup I did, I made no difference to my code when tested it on iOS 8 and iOS 9. Please check the GitHub link above. – bluenowhere Jan 04 '16 at 07:08
  • I have to set specific height constraints for the upper 2 labels to make the height automation work in iOS8, or simply have no more than 2 labels in one cell. Looking for a better way ... – bluenowhere Feb 26 '16 at 16:38
  • @bluenowhere Did you correct this problem ? I would like to know the solution as well. Please post an answer if you know. I am developing everything programmatically. – mythicalcoder Jun 17 '16 at 08:38
  • 1
    @Maven the comment below the accepted answer worked for me – bluenowhere Jun 23 '16 at 03:31
  • you have to set all left, right, bottom, and top constraints relative to cell container view – Hardik Darji Feb 16 '17 at 13:31
  • Swift 4.2/Xcode 10 UITableViewAutomaticDimension has been renamed to UITableView.automaticDimension `self.tbl.estimatedRowHeight = 60.0` `self.tbl.rowHeight = UITableView.automaticDimension` – Neeraj Joshi Mar 08 '19 at 09:52

6 Answers6

15

add following code in "cellForRowAtIndexPath" method before return cell.

    cell.setNeedsUpdateConstraints()
    cell.updateConstraintsIfNeeded()
hasankose
  • 303
  • 3
  • 10
6

To make it work on iOS 8 you need 3 things:

1) Set your table "estimated row height" on your view did load method.

tableView.estimatedRowHeight = 200.0

2) Implement the "table view height for cell at... method". (Remove the 'override' if your class is not a subclass of 'UITableViewController' and you are adding the table view yourself.

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

3) Call the 'updateConstraintsIfNeeded' method when you return your cell inside the 'table view cell for row at...' method.

cell.updateConstraintsIfNeeded()

return cell

Notice: Step 3 is NOT needed for iOS 9+

Pochi
  • 13,391
  • 3
  • 64
  • 104
4

Give Top,Bottom,Leading,Trailing Contraints to UILabel used. Check numberOfLine must be 0 Then set below methods

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewAutomaticDimension;}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
return UITableViewAutomaticDimension;}

Now run the project :)

Hemali Luhar
  • 349
  • 1
  • 11
3

Version 10.1

UITableViewAutomaticDimension is renamed to UITableView.automaticDimension

sushi
  • 157
  • 1
  • 7
2

This two lines did the trick for me:

tableView.estimatedRowHeight = 200.0
tableView.rowHeight = UITableViewAutomaticDimension

Everything else I tried from other answers didn't work.

Everton Cunha
  • 1,017
  • 8
  • 10
1

I can't make it work... I checked all hints in this thread but the table view cells don't resize their height.

I had the same custom table view cell set up as prototype cell in the table view in the storyboard and it worked.
For reasons of reusability, I set the cell up as xib. The table view loads and but the cells don't resize any more. It seems it works with storyboard but not with xib table view cells.
I have a UICollection view inside each tv cell with a varying number of cv cells, that's why the height of each tv cell cannot be the identical.

I didn't change any function calls, e.g. self.tableview.beginUpdates() and .endUpdates() is called in multiple places. I use Xcode 8.3.2

domi852
  • 497
  • 1
  • 4
  • 13
  • issue resolved. the problem was that I had set a fixed height for the title view of the tv cell... It seems that none of the view elements that have constraints directly with the tv cell's content view can have any fixed height constraint. That is straightforward for the collection view in the tv cell but in my opinion not so straightforward for the title view... Very important for the collection view: self.collectionView.translatesAutoresizingMaskIntoConstraints = true . – domi852 May 24 '17 at 08:28
  • And since the collection view in the tv cell has scrolling switched off, I had created this extension to update its height every time the tv cell lays out it subviews: extension UICollectionView { func updateHeight() { let updatedHeight = self.collectionViewLayout.collectionViewContentSize.height if updatedHeight != self.frame.size.height { let frame = CGRect(x: self.frame.origin.x, y: self.frame.origin.y, width: self.frame.size.width, height: updatedHeight) self.frame = frame } } } – domi852 May 24 '17 at 08:31