2013-03-12 31 views
0

我想从数据库中获取评论列表。评论可能甚至达到100线。问题是我无法让它断线。我用表格中的许多行

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

Curently测试的评论是:

looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong

,但它在中间,没有 “...”,并没有文字环绕结束。如何解决这个问题?

Btw。有很多像这样的细胞。

+2

你必须增加文本标签的高度也 – pdrcabrod 2013-03-12 11:35:49

+0

不能。那些没有那么多文本的字段将会有空的空间。它应该是动态的高度。 – 2013-03-12 11:37:29

+1

非常相似的问题。 http://stackoverflow.com/q/129502/767730 – Anupdas 2013-03-12 11:37:35

回答

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 

} 

在上面的代码中没有必要设置用于numberOfLinesUILabel

+0

使单元格和文本视图的高度不显示... – 2013-03-12 11:39:23

+1

这是在表格视图单元格中。标签本身可能会根据尺寸进行调整以适应表格单元格视图。但是表格总是只能提供如rowHeight属性给出的那么多的空间,或者像tableView:heightForRowAtIndexPath:所返回的那样。所以他不会来实际计算所需的大小,并从单元格的大小并通过实施tableView:heightForRowAtIndexPath提供exatyl: – 2013-03-12 11:51:42

+0

@ HermannKlecker-检查我的编辑。 – iPatel 2013-03-12 12:01:13

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 
0

你将不得不impement 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并在那里做布局。