Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Standard UITableViewCell with dynamic height

I want to know is it possible to have a standard UITableViewCell with style UITableViewCellStyleSubtitle having dynamic height?

If yes, then how to achieve that?

  • I do not want to customize and make a new class and nib for the cell.
  • I have set the numberOfLines property of cell.textLabel and cell.detailTextLabel to 0.

Alright, if your textLabel contains multiple line content and detailTextLabel contains single line content, then the tableView adjusts cell height automatically. But if it is other way round then it doesn't! Is this a bug by Apple? or expected functionality?

Below are the screen shots

enter image description hereenter image description here

like image 702
Burhanuddin Sunelwala Avatar asked Apr 22 '15 07:04

Burhanuddin Sunelwala


People also ask

What is Estimatedrowheight?

The estimated height of rows in the table view.

How do you find the height of a cell in Swift?

You need the cell for a certain IndexPath to calculate its bounds. You can do it this way in any of the delegate functions of the UITableView : let row = tableView. cellForRow(at: indexPath) let cellHeight = (row?.


1 Answers

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewAutomaticDimension;
}

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

    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
    cell.textLabel.text = @"Title";
    cell.detailTextLabel.text = @"SubTitle text SubTitle text SubTitle text SubTitle text SubTitle text SubTitle text SubTitle text SubTitle text SubTitle text SubTitle text SubTitle text SubTitle text SubTitle text SubTitle text SubTitle text SubTitle text SubTitle text";
    cell.detailTextLabel.numberOfLines = 0;
    return cell;
}
like image 131
Bhaumik Surani Avatar answered Oct 21 '22 08:10

Bhaumik Surani