UITableViewAutomaticDimension not working on iOS 8

五迷三道 提交于 2019-12-05 06:48:17

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 :)

Version 10.1

UITableViewAutomaticDimension is renamed to UITableView.automaticDimension

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!