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:
iOS 9 as I expected:
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()
add following code in "cellForRowAtIndexPath" method before return cell.
cell.setNeedsUpdateConstraints()
cell.updateConstraintsIfNeeded()
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+
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 :)
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.
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
Version 10.1
UITableViewAutomaticDimension is renamed to UITableView.automaticDimension