iOS: UITableView cells with multiple lines?

后端 未结 5 1399
既然无缘
既然无缘 2020-12-23 21:42

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 sty

5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-23 21:55

    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.

提交回复
热议问题