Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Self-Sizing Table View Cell in Xcode 9

Tags:

I have a UITableViewController where the cell's self sized correctly using Xcode 8 and Swift 3. Now that I'm using Xcode 9 and Swift 4, they aren't expanding and are just using the default height of 44.

(I have about a sentence or two in each UITableViewCell)

I was using this before:

// MARK: - Table view delegate  override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {     return UITableViewAutomaticDimension }  override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {     return UITableViewAutomaticDimension } 

... but was able to comment it out because per Updating Your App for iOS 11 said that the default would be self-sizing now:

enter image description here

I've tried playing around with changing the deployment target to iOS 11, playing around in Storyboard (but I'm using a Table View Cell style Basic so there is not much AutoLayout to be done), and I can't figure out what is going on.

I have the UILabel title set to 0 Lines, and have Line Break Word Wrap, but still not getting anywhere close to getting the cell to expand based on the text content inside of it in Xcode 9. Any ideas?

Thanks!

Edit:

Here's the options (that I don't have) for pinning since it is a Basic cell:

enter image description here

enter image description here

enter image description here

like image 614
SRMR Avatar asked Jun 08 '17 21:06

SRMR


1 Answers

I had the same problem and solved it with to lines of code:

class MyTableViewController: UITableViewController {   override func viewDidLoad() {     super.viewDidLoad()      tableView.estimatedRowHeight = UITableViewAutomaticDimension     tableView.rowHeight = UITableViewAutomaticDimension   } 

Maybe it is a bug in Xcode.

Update

New in Xcode 9 beta 3:

Interface Builder now supports setting the estimatedRowHeight of UITableView. This allows self-sizing table cells by setting the estimated height to a value other than zero, and is on by default. (17995201)

like image 191
Micha C. Avatar answered Sep 28 '22 04:09

Micha C.