45

What's the best way to have UITableView cells with multiple lines ? Let's say 5.. or 6 ?

Instead of textLabel and la detailTextLabel ? Should I create a custom style ? or a custom view ?

Any tutorial / example is well accepted.

thanks

aneuryzm
  • 63,052
  • 100
  • 273
  • 488

5 Answers5

132

You can use the existing UILabel views in the UITableViewCell for this. The secret is to do the following:

cell.textLabel.numberOfLines = 0;
cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;

By default, the UILabel only allows 1 line of text. Setting numberOfLines to 0 basically removes any limitations on the number of lines displayed. This allows you to have multiple lines of text.

The setting of the lineBreakMode to Word Wrap tells it to word wrap long lines of text onto the next line in the label. If you don't want this, you can skip that line.

You may also have to adjust the height of the table view cell as needed to make more room for the multiple lines of text you add.

For iOS 6.0 and later, use NSLineBreakByWordWrapping instead of UILineBreakModeWordWrap, which has been deprecated.

skagedal
  • 2,323
  • 23
  • 34
rekle
  • 2,375
  • 1
  • 18
  • 9
  • uhm, sounds good. I'm not been very accurate asking the question: I actually need to be precise in setting a next line, I can't just wrap. Should I add "\n" to the NSStrings ? – aneuryzm May 11 '11 at 13:53
  • Adding a '\n' (along with my above solution) should work to allow you to have multiple lines of text in the label. – rekle May 23 '11 at 12:40
  • 4
    How would you automatically adjust the height after doing this? – Navarr Jun 14 '11 at 21:06
  • In this way if i use UITableViewCellStyleValue1 the cell.textLabel overlaps the cell.detailTextLabel, there is some workaround without resize the cell height? – Mat Jul 12 '11 at 09:39
  • 24
    for iOS6 or higher use NSLineBreakByWordWrapping. (UILineBreakModeWordWrap is depracated) – Bocaxica Jan 08 '13 at 07:53
  • Perfecto ;) Nice,Straightforward, one run answer, Thanks :) – Atef Feb 13 '14 at 03:52
  • 1
    in my case, i needed to update interface builder/storyboard. i included 'cell.textLabel.numberOfLines = 0;' but lines only wrapped after finding cell 'Lines' in storyboard and changing it from '1' to '0'. i guess if code and storyboard conflict, app follows storyboard settings. – tmr Jan 10 '15 at 07:37
  • if I need the multi-lines within a cell to have different font sizes & weight, how can that be accomplished? I'm thinking something like in gmail or google mail apps. Thanks – rockhammer Jun 22 '16 at 22:16
28

Since Swift 3:

func allowMultipleLines(tableViewCell: UITableViewCell) {
    tableViewCell.textLabel?.numberOfLines = 0
    tableViewCell.textLabel?.lineBreakMode = .byWordWrapping
}
Gobe
  • 2,559
  • 1
  • 25
  • 24
1

There is a method to accomplish this just using storyboard. First, select the cell and go to the attributes section on the right panel. The first option should be 'Style'. Change this from custom to basic. Now, in your cell you should see text that says 'Title'. Double click it and in the right panel you should be able to set the number of lines.

1
cell.textLabel.numberOfLines = 0

together with

tableView.rowHeight = UITableView.automaticDimension

Does work but only if the number of lines is limited (2-3 lines).

What I had to do as well was embed the cell fields in a StackView. That made all the difference. Now I can display as many lines as I want.

Makwan Barzan
  • 353
  • 1
  • 6
  • 12
0

I found this worked for me on Xcode Version 8.0 (8A218a)

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath)    UITableViewCell {
        let cell = UITableViewCell()
       //MARK: word wrapping in cell
        cell.textLabel?.text = self.choices[(indexPath as NSIndexPath).row]
        cell.textLabel?.numberOfLines=0 // line wrap
        cell.textLabel?.lineBreakMode = NSLineBreakMode.byWordWrapping

        return cell
    }
lokusking
  • 7,396
  • 13
  • 38
  • 57