Incorrect text wrapping in UITableViewCell

I am having problems with my custom UITableView . I was wondering how to correctly make a group of text in a cell without seeing any ellipses "..." and without the text trimmed at the end of the cell.

This is what my cell looks like:

enter image description here

This is part of the UISplitViewController . The problem is that until for some reason it shows the entire length of the text, but it reaches the end of the cell and the rest of the line is turned off (this happens when I check "AutoLayout").

Here is what my code currently looks like:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"BCell"; BracketTableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if(cell == nil) { cell = [[BracketTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; [cell.description setLineBreakMode:NSLineBreakByWordWrapping]; cell.description.numberOfLines = 0; cell.description.font = [UIFont fontWithName:@"Helvetica" size:14.0]; } Bracket *bracket = [brackets objectAtIndex:indexPath.row]; [cell.description setText:bracket.name]; [cell.bracketId setText:[NSString stringWithFormat:@"%@", bracket.bracketId]]; return cell; } 

I am experimenting in height, but that doesn't seem to matter much, because I can set the height to everything, but it still shows truncated text.

Thanks!

+4
source share
4 answers

Usually my approach to supporting variable height cells is to define a class method that can calculate the size for a given model object:

 + (CGFloat)heightForBracket:(Bracket*)bracket; 

The beauty of creating a class method is that you can share constants (pad values, font sizes, indentation levels, etc.) with code that actually implements the layout without exposing them to other classes. If you want to change these constants in the future, you only need to make changes in one place in the cell subclass. Subclass implementation example:

 #define kPaddingHorizontal 10.0 #define kPaddingVertical 10.0 #define kFontSizeName 17.0 + (CGFloat)heightForBracket:(Bracket*)bracket { // determine the dimensions of the name UIFont *nameFont = [UIFont systemFontOfSize:kFontSizeName]; CGFloat nameSize = [bracket.name sizeWithFont:nameFont constrainedToSize:CGSizeMake(300, CGFLOAT_MAX) // 300 is the width of your eventual label lineBreakMode:NSLineBreakByWordWrapping]; // Apple recommends all cells be at least 44px tall, so we enforce a minimum here return MAX(44, nameSize.height + 20 + kPaddingVertical*2); // 20 is space for the subtitle label } - (id)initWithReuseIdentifier:(NSString *)reuseIdentifier { self = [super initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier]; if (self) { // bracket name self.textLabel.numberOfLines = 0; // 0 makes this variable height self.textLabel.font = [UIFont systemFontOfSize:kFontSizeName]; self.textLabel.lineBreakMode = NSLineBreakByTruncatingTail; self.textLabel.backgroundColor = [UIColor clearColor]; // if you wanted to hardcode a specific width, to a subview do it here as a constant and then share it with heightForBracket: // bracket number self.detailTextLabel.numberOfLines = 1; self.detailTextLabel.font = [UIFont systemFontOfSize:14.0]; self.detailTextLabel.lineBreakMode = NSLineBreakByTruncatingTail; self.detailTextLabel.backgroundColor = [UIColor clearColor]; } return self; } - (void)setBracket:(Bracket*)bracket { _bracket = bracket; self.textLabel.text = bracket.name; self.detailTextLabel.text = [NSString stringWithFormat:@"%@", bracket.bracketId]; } 

Then you can call heightForBracket: in tableView:heightForRowAtIndexPath: ::

 - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { Bracket *bracket = [brackets objectAtIndex:indexPath.row]; return [BracketTableCell heightForBracket:bracket]; } 

tableView:cellForRowAtIndexPath: becomes very simple, just set the appropriate cell in the cell:

 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"BCell"; BracketTableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[BracketTableCell alloc] initWithReuseIdentifier:CellIdentifier]; } Bracket *bracket = [brackets objectAtIndex:indexPath.row]; cell.bracket = bracket; return cell; } 

A few notes:

  • this assumes that the cell does not use automatic layout
  • this clearly hardcodes the width of the cell / label, which may or may not be suitable for your use.
  • you should never call the description property because it is a method that already exists in the NSObject protocol
  • Other improvements will cache the result of heightForBracket: to improve scroll performance, especially if you start to perform calibration logic for a ton of subview
+1
source

@gdubs you can use custom UITableViewCells

for reference you can use Customize Table Display Elements for UITableView

I think it would be easy for you to configure UILabels. for example, if you want to add mutilple lines, set TitletLabel.numberOfLines=0; , and if you want wordwrapping TitleLabel.lineBreakMode=NSLineBreakByWordWrapping; . There are other options for wrapping words.

0
source

The key to happiness with shortcuts and Autolayout is setting the preferredMaxLayoutWidth property on the shortcut. I think that without these labels you can’t wrap properly (or in general, in some cases, what you saw before).

Set the maximum line width, and then the labels should behave correctly.

0
source

I think the problem is with the width of your shortcut, if you use automatic layout, expand the width of the label to fill the parent cell, and add trailing and go to the supervisor so that it can be changed using it.

0
source

All Articles