borna
borna

Reputation: 924

Unable to fit multiple lines in tableview cell

I am trying to have a Title and multiple lines in a table view cell, however the cell is not expanding for the "title" and the 3 lines to fit in there.

Here is my code that is taking care of the title and the 3 lines

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("deviceCell", forIndexPath: indexPath)

    cell.textLabel?.text = "Title"
    cell.detailTextLabel?.text = "Line1 " + "\n" + "Line2" + "\n" + "Line3"

    return cell
}

This is how it looks after I run the program

enter image description here

enter image description here

Upvotes: 1

Views: 138

Answers (2)

moosa0709
moosa0709

Reputation: 476

Add this two new methods It's helps to solve this issues

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

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

Upvotes: 3

taipingeric
taipingeric

Reputation: 715

you have to change your cell height to AutomaticDimention

  • Use AutoLayout to set your Label Height

    in your case: you have to set Label height greater than 3 lines of text.

  • Set the tableView.rowHeight = UITableViewAutomaticDimension

  • tableView.estimatedRowHeight = YOUR_ESTIMATION_HEIGHT

then the cell will fit your content

Upvotes: 1

Related Questions