简体   繁体   中英

Setting UITableViewCell height and the cell's text label based on the text height

I have followed the instructions I have been able to find on stackoverflow to fix the following but none have worked. Below is my code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

static NSString *CellIdentifier = @"DoCCell";
DoCCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[DoCCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...

CGSize constraintSize = CGSizeMake(cell.infoLabel.frame.size.width, MAXFLOAT);
CGSize labelSize = [_content[[indexPath row] * 2] sizeWithFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:12.0f]
                                             constrainedToSize:constraintSize
                                                 lineBreakMode:NSLineBreakByWordWrapping];
CGRect frame = CGRectMake (cell.infoLabel.frame.origin.x, cell.infoLabel.frame.origin.y, labelSize.width, labelSize.height);
[cell.infoLabel setFrame:frame];

cell.infoLabel.lineBreakMode = NSLineBreakByWordWrapping;
cell.infoLabel.numberOfLines = 10;

_font = cell.infoLabel.font;
cell.infoLabel.text = _content[[indexPath row] * 2];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
CGSize constraintSize = CGSizeMake(280.0f, MAXFLOAT);

CGSize size = [_content[[indexPath row] * 2] sizeWithFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:12.0f]
               constrainedToSize:constraintSize
                   lineBreakMode:NSLineBreakByWordWrapping];
return size.height + 30.0;
}

but when I run my code the height of the cells is changed appropriately while the label size is not.

The cell is a custom cell and I have added the label via the .xib file. I tried manually stretching the label which worked in the sense that it would display all of the text so the issue is not in the wrapping of the label. I have also tested the cell.infoLabel.frame.size.height and the height value DOES change with the height of the cell as far as the value is concerned but it is not displayed as such. What am I doing wrong?

I think that the problem might be of adjusting the vertical alignment of the text in the UILabel instance.

I think you should do the following inside cellForRowAtIndexPath:

cell.infoLabel.text = _content[[indexPath row] * 2];
cell.infoLabel.numberOfLines = 0;
[cell.infoLabel sizeToFit];

More details about it can be found in the following link: Vertical Alignment of UILabel Text

Hope this helps! :)

Use below code in cellForRowAtIndexPath delegate of tableView :

    // getDynamicHeight function calculates height based on text length
    //Dynamically determine height for cell text
    CGFloat calculatedHeight = [self getDynamicHeight:@"Some very long text here"];  

    //Set name label frame based on text height
    cell.infoLabel.frame = CGRectMake(cell.infoLabel.frame.origin.x,cell.infoLabel.frame.origin.y, cell.infoLabel.frame.size.width,calculatedHeight);
    cell.infoLabel.numberOfLines = 5;

    cell.infoLabel.text = @"Some very long text here";

// getDynamicHeight function
//Dynamically determine height for cell based in containing Text 
-(CGFloat) getDynamicHeight : (NSString *) strCellTextName {

UILabel *lblGetDynamicHeight = [[UILabel alloc] init];
lblGetDynamicHeight.lineBreakMode = UILineBreakModeWordWrap;

lblGetDynamicHeight.text = strCellTextName;

CGSize labelStringSize = [lblGetDynamicHeight.text sizeWithFont:lblGetDynamicHeight.font constrainedToSize:CGSizeMake(142, 9999) lineBreakMode:lblGetDynamicHeight.lineBreakMode];

return  labelStringSize.height; }

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