如何在tableView单元格中显示多行 [英] How to display multiple lines in a tableView cell

查看:338
本文介绍了如何在tableView单元格中显示多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有点长的字符串,当显示在tablView单元格时会被截断。我这样做使表视图更宽:

I have a somewhat long string that gets truncated when displayed in a tablView cell. I did this to make the table view wider:

tableView.rowHeight = 100;

如何使字体变小以及在表格视图单元格中包装文本? p>

How can I make the font smaller as well as wrap the text in the table view cell?

推荐答案

tableView:cellForRowAtIndexPath: (或descriptionLabel,取决于您使用的单元格的样式)来执行此操作。设置字体更改字体, linkBreakMode 使其自动换行, numberOfLines 设置多少行的最大值(在这之后它会被截断。你可以将它设置为0,表示没有最大值。

In tableView:cellForRowAtIndexPath: you can set a few properties on the textLabel (or descriptionLabel, depending on what style of cell you're using) to do this. Set font to change the font, linkBreakMode to make it word-wrap, and numberOfLines to set how many lines max (after which point it truncates. You can set that to 0 for no max.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell* aCell = [tableView dequeueReusableCellWithIdentifier:kMyCellID];
    if( aCell == nil ) {
        aCell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kMyCellID] autorelease];

        aCell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:10.0];
        aCell.textLabel.lineBreakMode = NSLineBreakByWordWrapping; // Pre-iOS6 use UILineBreakModeWordWrap
        aCell.textLabel.numberOfLines = 2;  // 0 means no max.
    }

    // ... Your other cell setup stuff here

    return aCell;
}

这篇关于如何在tableView单元格中显示多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆