表格单元格中有很多行

时间:2013-03-12 11:34:32

标签: iphone ios objective-c

我正在尝试从数据库中获取评论列表。评论甚至可能达到100行。问题是我不能让它破坏。我用过

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

测试评论肯定是:

  

looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong

但它在中间结束,没有“......”并且没有文本换行。如何解决这个问题?

顺便说一下。有很多像这样的细胞。

3 个答案:

答案 0 :(得分:0)

#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

}

在上面的代码中,无需为numberOfLines设置UILabel

答案 1 :(得分:0)

在tableview委托中执行此操作:

- (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

答案 2 :(得分:0)

你必须要求tableView:heightForRowAtIndexPath: 有关详细信息,请参阅http://developer.apple.com/library/ios/#documentation/uikit/reference/UITableViewDelegate_Protocol/Reference/Reference.html

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

这里有关于如何使用的示例 - sizeWithFont:constrainedToSize:lineBreakMode:

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