Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate the table's contentSize.height with custom cells of different types iphone

In my application , the tableview is having different types of custom cells with different heights. I want to put a view below this tableView, for which I need to calculate the table contentView height, how can I do it?

Earlier I used to do it like: (noOfRows * cell_height), but that was for static heights.

Thanx in advance.

like image 297
neha Avatar asked Jul 12 '10 19:07

neha


4 Answers

UITableView is a subclass of UIScrollView, therefore you can use the instance method contentSize on your tableview:

yourtableView.contentSize.height

Should give you the height of your tableview content view

like image 148
ErezSO Avatar answered Oct 09 '22 21:10

ErezSO


This is an old question, but, today I've solved my problem with this

  func viewDidLoad() { 
      super.viewDidLoad()

      tableView.rowHeight = UITableViewAutomaticDimension
      tableView.estimatedRowHeight = 44 //  >= 2
    }     

  func getTableViewHeight() -> CGFloat{ 
      tableView.reloadData()
      tableView.layoutIfNeeded()

      return tableView.contentSize.height + tableView.contentInset.bottom + tableView.contentInset.top
    }
like image 45
Amir Khorsandi Avatar answered Oct 09 '22 22:10

Amir Khorsandi


i have calc height in viewDidLayoutSubviews() for content view controller. Work for me (table view automatic cell dimensions);

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews();
    let height = table.contentSize.height+table.contentInset.bottom+table.contentInset.top
    preferredContentSize = CGSize(width: 305, height: height);
}
like image 31
john07 Avatar answered Oct 09 '22 20:10

john07


The header and footer of the section are not considered in the height of the table, consider this

CGSize sz = self.table.contentSize;
sz.height += self.table.contentInset.bottom +
             self.table.contentInset.top + 
             self.table.numberOfSections * self.table.sectionHeaderHeight;
like image 42
Roman Solodyashkin Avatar answered Oct 09 '22 21:10

Roman Solodyashkin