0

I'm having a UITableView. In that I have a single label. I am doing following for label:

  1. Set number of lines to zero

  2. Set top space to container margin

  3. Set bottom space to container margin

My contents for UILabel may vary, some time it may have 3 lines, some times 10 lines etc. My problems is it displays only 2 rows. How to adjust the height of cell so that UILabel will show the whole contents.

Thanks in advance.

Bhavin Ramani
  • 3,221
  • 5
  • 30
  • 41
e.k
  • 1,333
  • 1
  • 17
  • 36

2 Answers2

2

You should set numberoflines to 0. Provide either leading & trailing space constraints for your label or fixed width constraint along with top & bottom space constraints. Then use tableView.estimatedRowHeight = 44.0 tableView.rowHeight = UITableViewAutomaticDimension

Maulik Bhuptani
  • 597
  • 3
  • 14
0

set your label's constraint with cell like top bottom leading and trailing, set number of line for labels to zero and line break mode to word wrap. now place this two tableView methods to calculate cell height dynamically.

For Swift 3.0

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
} 

for Objective C

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

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

so it will take cell size dynamically as you want.

hope this will help you.

Pankaj K.
  • 535
  • 8
  • 18