4

I am using custom tableview cell with three labels title,date and description, now I am getting data from web-services, in which some description text is too big to adjust within description label's frame

how to proceed with it ?

Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
Amitkumar
  • 53
  • 1
  • 4

4 Answers4

3

simply use like

ObjectiVe-C

- (void)viewDidLoad
{
...
...
self.tableView.estimatedRowHeight = 100.0;
self.tableView.rowHeight = UITableViewAutomaticDimension;
}

Swift

 override func viewDidLoad() {


        self.tableView.estimatedRowHeight = 80
        self.tableView.rowHeight = UITableViewAutomaticDimension

}

ObjectiVe-C

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 80; // customize the height
}

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

Swift

 func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 80 // customize the height
}

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}

finally set your label.numofLines = 0 in attribute or programtically

Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
3

I would like to know why to know you're looking for a method without using autolayout. I believe previous posted answers are solutions using autolayout

With Autolayout

self.tableView.estimatedRowHeight = 50;
self.tableView.rowHeight = UITableViewAutomaticDimension;

and you can set multiline label to expand according to the content of the label

Without Autolayout

Without autolayout you need to calculate height for each cell (height for each labels + margins) individually and return the value in UITableView datasource method - tableView:heightForRowAtIndexPath:.

Community
  • 1
  • 1
Dickson Leonard
  • 518
  • 5
  • 14
1

write to viewDidLoad

tableView.estimatedRowHeight = 50
tableView.rowHeight = UITableViewAutomaticDimension
Phu Duy
  • 187
  • 1
  • 8
0

Thanks but issue is not resolved Please check images

  1. Before without using above solutions My table view looks better but text is not adjusted

  2. after using your help

enter image description here

using self.tableView.rowHeight = UITableViewAutomaticDimension; and heightforcell method everything is messed

My tableview with storyboard please check size inspector tab for autoresizing section

enter image description here

Whats the wrong am i doing here ?

Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
Amitkumar
  • 53
  • 1
  • 4
  • you have to set line number of label to 0, do you use `self.tableView.estimatedRowHeight = current row height` ` – Phu Duy Jul 20 '16 at 15:45