Multiple Label Lines in a Custom UITableviewCell

I was looking for any advice for my problem. But I can not find a solution for this.

I created a subclass of UITableviewCell (FeedCell). With one image and two labels. The problem is that the shortcut, which should be multi-line, does not appear with multi-lines.

I use autolayot.

This is an application that displays twitterfeed users.

My code is:

- (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 also installed 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 display with multi-lines. I have not tried this with another CellStyle (I use a custom cell style).

Someone tell me?

+4
source share
5 answers

For mutiline, use the following:

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

for testing purposes, set the line height to 46.0f in the following way:

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

I could not fix the height problem, but it gave me a UILabel with a few lines

+2
source

I know this is an old post, but it appeared when I searched.

I got such an example, following http://www.raywenderlich.com/73602/dynamic-table-view-cell-height-auto-layout .

I think the following is required for iOS8:

  • Setting lines to 0
  • Word wrap setting
  • Setting the tag size β†’ 20
  • Make sure there are enough restrictions to determine the height of the cell (header height and vertical spacing)
+2
source

to try

 [tweetCell.tweetText sizeToFit] 
+1
source

First, if you want to show 2 lines of text (minimum 1 and maximum 2), the value of numberOfLines should be set to 2. Setting it to 0 means no restrictions.

Secondly, it is not enough to indicate the number of lines. Tag width must be specified. Either use sizeToFit , or set a constant value.

0
source

Try putting code that sets the number of lines, linebreakmode and font OUTSIDE from these curly braces

0
source

Source: https://habr.com/ru/post/1500371/


All Articles