Jack Chorley
Jack Chorley

Reputation: 2344

Auto-Resizing tableViewCells in iOS8 still truncate UILabel text

I have been trying to use the new way to resize UITableView cells in iOS8 and to an extent, it works. I first make sure that the label is attached to the cell's container view at the top, bottom, trailing side and leading side which i was told to do on an online tutorial, though i feel that this is where the problem lies. In the viewDidLoad, I then put in the following lines:

self.tableView.estimatedRowHeight = 56
self.tableView.rowHeight = UITableViewAutomaticDimension

After watching the WWDC, it appears that this is pretty much all I need in regards to the cell, so then i went into the interface builder and set the "lines" property for the label to 0, again as instructed in the video. It resizes all of the cells as I would expect though it always makes them one line too small so that the full text doesn't fit in. Below is a screenshot of what it looks like. Please ask if you need more information or photos if it makes it easier for you to give me some advice. I realise this is probably a stupid error on my part but there are few places to look as iOS 8 is so new. Thanks a lot

enter image description here

Upvotes: 3

Views: 2325

Answers (3)

donny
donny

Reputation: 469

I had the same problem;

First, make sure content hugging priority is greater than another element. Second, call tableView.reloadData() in viewDidLoad().

override func viewDidAppear(animated: Bool) {
     super.viewDidAppear(animated)
     tableView.reloadData()
}

Finally, define the UITableViewDataSource methods to make the height dynamic:

override func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return tableView.rowHeight
}

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

Upvotes: 0

Amos
Amos

Reputation: 182

I had the same problem. I had to do all 3 suggestions to get it working:

Set the number of lines on the labels to 0.

In cellForRowAtIndexPath: set the cell labels' text to an empty string before assigning the desired string.

calling [tableView reloadData] in viewDidAppear:.

I did try doing them individually and it doesn't work.

I had a minor issue after that however...The original layout of the text is in view before the table reloads and causes a weird looking text "flicker" when the text gets adjusted. So I created a bool and set the cell labels to hidden. When viewWillAppear: gets called i set that bool to true and in cellForRowAtIndexPath: I unhide the labels if the bool is true.

In any case hopefully they get this fixed because it's an awesome feature.

Upvotes: 1

mark w
mark w

Reputation: 1

I've also run into formatting issues with Auto-Resize, with UILabel objects in particular. What finally worked for me was to modify cellForRowAtIndexPath: and first assign the UILabel's text to an empty string before assigning the actual string value, like this:

cell.myLabel.text = @"";
cell.myLabel.text = @"Here is the label's actual string value";

I have a feeling Apple still has some issues to work out with Auto-Resize.

Upvotes: 0

Related Questions