简体   繁体   中英

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). 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.

This is an app who display the users 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:

- (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. I have not tried this with another CellStyle (I use custom 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:

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

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 .

I think for iOS8 the following is required:

  • Setting the lines to 0
  • Setting the word wrap
  • Setting the label size to be >= 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.

Secondly, setting just the number of lines is not enough. The label width HAS to be specified. Either use sizeToFit , or set a constant value.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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