Custom Tableview cell with multiline UILabel needs dynamic height

I created a custom TableView cell with Interface Builder. This is what it looks like:

IB Custom Table Cell

  • iOS - failed to obtain a cell from its dataSource
  • UIRefreshControl - Pull To Refresh in iOS 7
  • Lazy load images in UITableView
  • UITableViewCell expand on click
  • SearchDisplayController SearchBar overlapping navigation bar and resizing itself IOS7
  • how to set the selected check mark as ticked in tableview cell swift 3.0
  • For the Description Label, I need that to word wrap so I set it as such:

    Settings for Description Label

    In my SettingsPageViewController, I have the following Table View methods overridden:

    @implementation SBSettingsViewController
    {
        NSArray *settingHeaders;
        NSArray *settingDescriptions;
    }
    - (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupLeftMenuButton];
    // Do any additional setup after loading the view from its nib.
    settingHeaders = [NSArray arrayWithObjects:@"Label Header 1",@"Label Header 2",nil];
    settingDescriptions = [NSArray arrayWithObjects:@"Two line description Two line description Two line description ",@"One Line Description",nil];
    }
    
    - (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
    return [settingHeaders count];
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *simpleTableIdentifier = @"SettingsTableCell";
    
    SettingsTableViewCell *cell = (SettingsTableViewCell *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"SettingsTableCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }
    
    cell.settingsHeaderLabel.text = [settingHeaders objectAtIndex:indexPath.row];
    cell.settingsDescriptionLabel.text = [settingDescriptions objectAtIndex:indexPath.row];
    
    cell.settingsDescriptionLabel.lineBreakMode = NSLineBreakByWordWrapping;
    cell.settingsDescriptionLabel.numberOfLines = 0;
    CGRect appFrame=[[UIScreen mainScreen] bounds];
    cell.settingsDescriptionLabel.preferredMaxLayoutWidth = appFrame.size.width - 15;
    
    [cell.settingsDescriptionLabel sizeToFit];
    [cell.settingsDescriptionLabel setNeedsDisplay];
    [cell layoutIfNeeded];
    
    return cell;
    }
    
    -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    
    if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [tableView setLayoutMargins:UIEdgeInsetsZero];
    }
    
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    }
    
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    return 85;
    }
    

    Here is what the resulting page looks like:

    enter image description here

    As you can see, the description label for the 1st tableview cell does NOT word wrap. It just cuts off. How do I make it wrap?

    Also, I’d like for the Tableview cell height to be dynamically adjusted. I tried to change the heightForRowAtIndexPath: to be UITableViewAutomaticDimension but that just made it look super weird as such:

    enter image description here

    How do I get the tableview height adjusted for the row with the 1 line description label is slightly shorter?

    Thanks you for your help!

    Solutions Collect From Internet About “Custom Tableview cell with multiline UILabel needs dynamic height”

    give constraint of header label top, leading, trailing to super view and bottom (vertical ) to description Label.
    same for description label > leading, trailing and bottom margin to super view.

    now set Header label height Fix and description label make multiline ( Lines = 0)

    for table view set in viewDidLoad

    _tableView.estimatedRowHeight = 100.0;
    _tableView.rowHeight = UITableViewAutomaticDimension;
    

    Let me know if this works…