表格单元格中有许多行



我正试图从数据库中获取一个注释列表。评论可能多达100行。问题是我不能让它断线。我用过

comment.adjustFontSizeToFitWidth = NO;
comment.numberOfLines = 0;
comment.lineBreakMode = UILineBreakModeCharacterWrap

目前的测试评论是:

loooooooooooo

但它在中间结束,没有"…",也没有换行符。如何解决此问题?

Btw。有很多这样的细胞。

在您的表视图委托中执行以下操作:

- (CGFloat)tableView:(UITableView *)tableView1 heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
        CGSize maximumSize = CGSizeMake(480.0,1000.0); // Put appropriate required height and width.
        NSString *dateString = [NSString stringWithFormat:@"%@",yourString];
        UIFont *dateFont = [UIFont fontWithName:@"Helvetica" size:14];
        CGSize dateStringSize = [dateString sizeWithFont:dateFont 
                                       constrainedToSize:maximumSize 
                                           lineBreakMode:UILineBreakModeWordWrap];
        return dateStringSize.height;
}

此代码将为您的单元格设置适当的高度。然后在您的cellForRowAtIndexPath函数中。保留这个代码:

comment.adjustFontSizeToFitWidth = NO;
comment.numberOfLines = 0;
comment.lineBreakMode = UILineBreakModeCharacterWrap

您将不得不阻抗tableView:heightForRowAtIndexPath:看见http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewDelegate_Protocol/Reference/Reference.html详细信息。

在该方法中,您必须根据标签显示文本所需的高度来计算每个单元格所需的身高。在NSString的UIExtensions中,您将找到用于该计算的辅助方法。http://developer.apple.com/library/ios/#documentation/uikit/reference/NSString_UIKit_Additions/Reference/Reference.html

下面是关于如何使用–sizeWithFont:constrainedToSize:lineBreakMode:的示例。

然后,您可以在cellForRowAtIndexPath中布局单元格项,在这里您需要再次使用sizeWithFont:...来计算文本的大小。或者,如果您想要一个整洁的解决方案,最好将UITableViewCell子类化并覆盖其layoutSubviews方法,然后在那里进行布局。

#define FONT_SIZE 11.0f
#define CELL_CONTENT_WIDTH 157.0f
#define CELL_CONTENT_MARGIN 10.0f
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
        NSString *text = YorCommentString;// or any String that u want.
        CGSize constraint = CGSizeMake(CELL_CONTENT_WIDTH - (CELL_CONTENT_MARGIN * 2), 20000.0f);
        CGSize size = [text sizeWithFont:[UIFont systemFontOfSize:FONT_SIZE] constrainedToSize:constraint lineBreakMode:UILineBreakModeWordWrap];
        CGFloat height = MAX(size.height, 44.0f); // set as u want 
        return height + (CELL_CONTENT_MARGIN * 2); // set as u want
}

在上面的代码中,UILabel不需要设置numberOfLines

最新更新