简体   繁体   English

自定义UITableviewCell中的多个标签行

[英]Multiple lines of a label in a custom UITableviewCell

I have searched around for any tip for my problem. 我一直在寻找我的问题的任何提示。 But I cannot find a solution for this. 但我无法找到解决方案。

I have made a subclass of UITableviewCell (FeedCell). 我已经创建了UITableviewCell(FeedCell)的子类。 With one image and two labels. 有一个图像和两个标签。 The problem is that the label I need to be multiline does not show up with multilines. 问题是我需要多线的标签不会出现多线。

I use autolayot. 我用autolayot。

This is an app who display the users twitterfeed. 这是一个显示用户twitterfeed的应用程序。

My code: 我的代码:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";

FeedCell *tweetCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (tweetCell == nil) {
    tweetCell = [[FeedCell alloc]
            initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    [tweetCell.tweetText setNumberOfLines:0];
    [tweetCell.tweetText setLineBreakMode:NSLineBreakByWordWrapping];
    [tweetCell.tweetText setFont:[self fontForCell] ];


}
NSDictionary *tweet = _dataSource[[indexPath row]];

NSString *tweetString = [tweet valueForKey:@"text"];

tweetCell.name.text =[tweet valueForKeyPath:@"user.name"];


[tweetCell.tweetText setText:tweetString];

return tweetCell;

} }

I have also set the heigthforRowAtIndexPath: 我还设置了heigthforRowAtIndexPath:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSDictionary *tweet = _dataSource[[indexPath row]];
NSString *theText=[tweet valueForKey:@"text"];
UIFont *cellFont = [self fontForCell];
CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);
CGSize labelSize = [theText sizeWithFont:cellFont constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping];

return labelSize.height + 20;

} }

The problem is that the tweet cell.tweetText does not show up with multilines. 问题是tweet cell.tweetText没有显示多行。 I have not tried this with another CellStyle (I use custom cellstyle). 我没有尝试过另一个CellStyle(我使用自定义cellstyle)。

Any tip anyone? 有人提示吗?

For mutiline use the following: 对于多行使用以下内容:

tweetCell.tweetText.numberOfLines = 0;
[tweetCell.tweetText sizeToFit];

for testing purpose set the height of row as 46.0f in the following method: 出于测试目的,在以下方法中将行的高度设置为46.0f:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

I could not get the height issue fixed but this did give me a UILabel with multiple lines 我无法修复高度问题,但这确实给了我一个多线的UILabel

I know this is an old post, but it came up when I was searching. 我知道这是一个很老的帖子,但是在我搜索的时候就出现了。

I got an example like this by following http://www.raywenderlich.com/73602/dynamic-table-view-cell-height-auto-layout . 我通过http://www.raywenderlich.com/73602/dynamic-table-view-cell-height-auto-layout得到了这样的例子。

I think for iOS8 the following is required: 我认为对于iOS8,需要以下内容:

  • Setting the lines to 0 将行设置为0
  • Setting the word wrap 设置自动换行
  • Setting the label size to be >= 20 将标签大小设置为> = 20
  • Making sure there are enough constraints to determine the cell height (height of title and vertical spacing) 确保有足够的约束来确定单元格高度(标题高度和垂直间距)

尝试

[tweetCell.tweetText  sizeToFit]

Firstly, if you want to show 2 line of text (minimum 1 and maximum 2), the numberOfLines must be set to 2. Setting it to 0 means no limit. 首先,如果要显示2行文本(最小1和最大2),则numberOfLines必须设置为2.将其设置为0表示没有限制。

Secondly, setting just the number of lines is not enough. 其次,仅设置行数是不够的。 The label width HAS to be specified. 要指定的标签宽度HAS。 Either use sizeToFit , or set a constant value. 使用sizeToFit或设置常量值。

尝试使用设置这些花括号的行数,换行符和字体OUTSIDE的代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM