1

Im using UITableView inside of a UITableViewCell. I want the cell to resize according to the content size of the tableView.

I have tried to get height of every cell by

 CGRect frame = [self.Table rectForRowAtIndexPath:indexPath];

but it is not returning me the correct height for cells. These cells are getting dynamic height according to text.

Ramon
  • 1,415
  • 9
  • 18
Ahmad
  • 118
  • 7
  • refer : http://stackoverflow.com/questions/18746929/using-auto-layout-in-uitableview-for-dynamic-cell-layouts-variable-row-heights – jerfin Jan 17 '17 at 09:08
  • http://stackoverflow.com/a/39888662/6656894 refer this answer – Himanshu Moradiya Jan 17 '17 at 09:14
  • My cells are getting dynamic height, i want my tableview to resize according to size of cells, as i want to disable the scroll of tableview. – Ahmad Jan 17 '17 at 09:33
  • @Ahmad there are lots of answers of your question is already present in stack overflow and one of the best suggested answer given by Arun , follow it. – dahiya_boy Jan 17 '17 at 09:42
  • @the_dahiya_boy my issue is same as this link [link](http://stackoverflow.com/questions/38229297/dynamically-set-uiscrollview-and-uitableview-height-by-its-content) – Ahmad Jan 17 '17 at 09:45
  • @Ahmad, how many cells do you have? – KrishnaCA Jan 17 '17 at 10:11
  • @KrishnaCA number of cells depend on data returned by API – Ahmad Jan 17 '17 at 10:12
  • You want to vary the height of `UITableView` right? Given that at any given point, your `total height of all cells` > `UIScreen.main.bounds.size.height`, I believe that it's better to fix the size of UITableView and enable it's scroll – KrishnaCA Jan 17 '17 at 10:15

2 Answers2

0

In viewDidLoad method

self.table.estimatedRowHeight = 44.0 ;
self.table.rowHeight = UITableViewAutomaticDimension;

//numberOfLinesOfYourLabel should be 1

and then

-(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewAutomaticDimension
}
mfaani
  • 33,269
  • 19
  • 164
  • 293
Dishant Rajput
  • 1,329
  • 1
  • 10
  • 20
0
-(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
   if(tableView == MainTableView){
 //Calculate height for cell for perticular indexpath of main Tableview
   }
   else if(tableView == subTableView){
  //Calculate height for cell for perticular indexpath of main Tableview  } }
  • In cellForRow method of mainTableview reload subtableview.
Punit
  • 1,330
  • 6
  • 13
  • My cells are getting dynamic height depending on text, setting height is not an option as im using 4 different type of cells and every cell ha 2-3 labels with different font and font size. – Ahmad Jan 17 '17 at 13:37
  • yes you have to calculate cell's height according to text and font in heightForRow method – Punit Jan 17 '17 at 13:40