3

I have a UITableViewCell with two UILabels, Using Storyboard and Autolayout. The problem is that the second label is displaying only two lines of text, even if there are more lines to show.

Edit: The issue happens when the controller is a UIViewController rather than a TableViewController, it works as expected within a TableViewController

I have set the numberoflines property to 0 In autolayout, I have pinned both the labels from all four sides.

But it didn’t help and only one line was displayed.

When I used heightForRowAtIndexPath along with systemLayoutSizeFittingSize to get the height of the row, thing improved marginally and two lines were displayed.

TableViewCell with two labels

I tested on both 8.2 and 8.4

I have gone tried the solutions outlined in this stack overflow Multiple UILabels inside a self sizing UITableViewCell

I have tried the steps outline in this tutorial: http://www.raywenderlich.com/73602/dynamic-table-view-cell-height-auto-layout

The code is here:https://www.dropbox.com/sh/jp8ki0uxvfuf6ng/AABZlzzLnw9dMoamNbNkI_6Ba?dl=0

Community
  • 1
  • 1
runios
  • 384
  • 1
  • 2
  • 14
  • I didn't check all the links you posted but adding some code would be great. Have you tried using UITableViewRowHeight = UITableViewAutomaticDimension? – Fred Faust Sep 02 '15 at 15:49
  • 1
    If you set the UITableViewHeight more bigger value, can you the rest of UILabel? – pixyzehn Sep 02 '15 at 15:57
  • @pixyzehn yes if I set the cell height to a bigger value, the text in the label lays out properly. – runios Sep 02 '15 at 17:50
  • @thefredelement the code is not much and is here https://www.dropbox.com/sh/jp8ki0uxvfuf6ng/AABZlzzLnw9dMoamNbNkI_6Ba?dl=0 . Btw, UITableViewAutomaticDimension doesn't help. – runios Sep 02 '15 at 17:58

2 Answers2

2

I was able to get this done like this:

Create leading, top and trailing constraints for your top label Create leading, bottom and trailing constraints for your bottom label

Create a vertical constraint between the two labels

Make sure your trailing constraint is set to use most of the width of the cell

enter image description here

Set the number of lines for the UILabel to 0

In your code:

import UIKit

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

var textData = ["Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum euismod nisl neque, et eleifend felis dictum in. Nullam semper diam nec leo malesuada, non ultrices eros bibendum. Phasellus laoreet eros quis enim condimentum, nec pharetra augue ultrices. Nunc quis convallis magna. Fusce fringilla diam nunc, in consequat urna accumsan nec. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Cras in diam nec dolor tristique efficitur sed eu est.", "Maecenas ipsum nunc, ultricies non faucibus vitae, eleifend sit amet felis. Donec non dignissim sapien. Etiam vitae dapibus dolor. Duis nec diam sed augue aliquam vehicula. Interdum et malesuada fames ac ante ipsum primis in faucibus. Sed vulputate gravida tellus. Sed porttitor lacus ac venenatis fringilla. Pellentesque in sapien felis. Vestibulum pharetra fermentum arcu in sollicitudin. Phasellus convallis sit amet felis in rhoncus. Fusce consectetur tempus varius. Aliquam a fermentum dui. Curabitur vestibulum varius purus. Duis feugiat ligula a ligula imperdiet porta.","Quisque sagittis elementum odio vitae convallis. Nam dignissim pellentesque leo quis feugiat. Suspendisse a nisi et metus posuere dignissim sollicitudin nec lorem. Cras nec porta urna, sit amet bibendum dolor. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Maecenas enim dui, pellentesque in pulvinar non, faucibus sodales nibh. Mauris non condimentum ipsum.","Sed volutpat lectus id nibh scelerisque, ac mollis lectus cursus. Integer orci neque, euismod et dolor quis, condimentum pulvinar arcu. Etiam finibus nibh at dapibus fringilla. Proin viverra lobortis tempor. Vivamus luctus sem sed purus elementum, et tempor erat mollis. Duis cursus massa in mi imperdiet, condimentum semper est fringilla. Mauris a elit hendrerit, finibus felis sit amet, placerat nulla. Integer convallis massa vitae magna fermentum, id auctor neque varius. Aliquam vestibulum a lacus eu efficitur. Donec condimentum nunc eu ante vehicula aliquet. Vestibulum congue magna ut ultricies tempus. Phasellus porttitor at nisi sed tincidunt.","Curabitur lobortis non augue quis lacinia. In fermentum porta eleifend. Pellentesque eu ligula sit amet neque mollis varius vitae lacinia purus. Phasellus ut gravida elit. Maecenas id sem luctus, iaculis massa eget, condimentum mi. Vestibulum venenatis iaculis dignissim. Curabitur erat dui, eleifend ac turpis sit amet, viverra imperdiet metus. Integer eleifend a velit id bibendum."]

@IBOutlet weak var tableView: UITableView!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    tableView.estimatedRowHeight = 60 // The # from storyboard in the size inspector
    tableView.rowHeight = UITableViewAutomaticDimension
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return textData.count
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! CTableViewCell

    cell.topLabel.text = ""
    cell.bottomLabel.text = ""

    cell.topLabel.text = "Cell Title"
    cell.bottomLabel.text = textData[indexPath.row]

    cell.bottomLabel.sizeToFit()

    return cell

}

}

And you'll get this:

enter image description here

Fred Faust
  • 6,696
  • 4
  • 32
  • 55
  • Thanks for the response. I tried your suggested method and I get the same result, i.e. the last line is not getting displayed. If I increase the amount of text, the number of lines displayed increases, but the last line doesn't get displayed. Maybe some problem with my auto layout constraints ... – runios Sep 03 '15 at 06:17
  • I did a few more tests and figured that the issue happens when the controller is a UIViewController rather than a TableViewController, it works alright within a TableViewController. – runios Sep 03 '15 at 07:31
  • Thanks this is working for me now. I had to reload my cell at the index path after updating the text label. I have marked your answer as the one which works for me. Setting the estimatedRowheight was the key though, without which auto layout always set the cell height to 44. – runios Sep 04 '15 at 09:51
0

You actually have 2 unknown heights: the height of the UILabel and the height of the UITableViewCell.

I guess the problem is: the height of the UITableViewCell depends on the height of the UILabel, but the height of the UILabel also depends on the height of the UITableViewCell...

In your code, you are trying to configure the cell (in heightForRowAtIndexPath) and calculate the cell height. BUT your UILabel height is still not the height you wanted.

I will calculate the height of the UILabel alone (in heightForRowAtIndexPath) to eliminate one unknown and determine the desired height for the UITableViewCell (height of first UILabel + spaces + height of second UILabel). Once that is done, the height of the second UILabel will be auto-adjusted in cellForRowAtIndexPath since the cell height is already known.

Hope this helps.

See bold text for the edit.

Rick
  • 1,818
  • 1
  • 15
  • 18
  • if heightForRowAtIndexPath returns the height of a first UILabel + spaces, this height will be less than the total height. How does the auto-adjustment work in cellForRowAtIndexPath? Can you share some code showing this? – runios Sep 02 '15 at 17:55
  • Sorry, need to add the height of the second UILabel as well. See edit. The auto-adjustment depends on your auto-layout. I will align the top of second UILabel to first label and the bottom to the bottom of the cell. When the cell height changes, the height of the second UILabel will be adjusted. – Rick Sep 03 '15 at 05:18
  • I got the table heights using the below code, but it displays upto two lines only, the third line doesn't show. I factored for the space between the labels and the cells as well. I am not sure what I am doing wrong here. sizingCell.workhoursLabel.sizeToFit() sizingCell.tableViewCellLabel.sizeToFit() var workhoursheight = sizingCell.workhoursLabel.frame.height var tableheight = sizingCell.tableViewCellLabel.frame.height – runios Sep 03 '15 at 06:07
  • ok, what I figured is that the last line is not getting displayed. If I increase the amount of text, the number of lines displayed increases, but the last line doesn't get displayed. Maybe some problem with my auto layout constraints ... – runios Sep 03 '15 at 06:16
  • I did a few more tests and figured that the issue happens when the controller is a UIViewController rather than a TableViewController, it works alright within a TableViewController. – runios Sep 03 '15 at 07:31
  • 1
    SizeToFit won't work with Auto Layout most of the time. It's usually a conflict between a manual and an auto means. When using Auto Layout, auto will win. Try using boundingRectWithSize to calculate the height instead of using sizeToFit and getting its frame.height. I believe it doesn't resize to the correct height when auto layout has constraints on it. – Rick Sep 03 '15 at 14:17