Reputation: 3325
Is there a way to find out the height of the content of a table view, beside adding up the height of all cells from all sections?
I have 3 sections each with different height cells and variable number of cells. So I am hoping for a system method to get the height of the content of the table view.
EDIT: nowhere in the answer of the other similar question does it say where to call the tableView.contentSize
Upvotes: 2
Views: 16258
Reputation: 870
Swift 4,5
1.Set tableview height outlet
2.Now add IBOutlet of height Constraint like below code.
@IBOutlet weak var tableviewHeightConstraints: NSLayoutConstraint!
3.Now reload tableview code in viewWillAppear method.
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
tableview.reloadData()
}
4.Now using get the tableview content size and set into willDisplayCell as tableview height.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("myTableViewCell", forIndexPath: indexPath) as! myTableViewCell
return cell
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
//Content wise tableview height
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
tableviewHeightConstraints.constant = tblData.contentSize.height
}
Upvotes: 2
Reputation: 351
Don't forget if label data is more then 1 line then set lable line is 0.
override func viewDidLoad() { super.viewDidLoad() //Reload Tableview tblData.reloadData() }
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("myTableViewCell", forIndexPath: indexPath) as! myTableViewCell
return cell
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
//Content wise tableview height
func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
constTblDataHeight.constant = tblData.contentSize
}
Upvotes: -1
Reputation: 847
You can retreive the content size by :
Swift / ObjC :
let contentSize : CGSize = self.tableView.contentSize
let width = self.tableView.contentSize.width
let height = self.tableView.contentSize.height
You can use this to know if your UITableView
is loaded (Swift 2.2):
override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
if indexPath.row == (tableView.indexPathsForVisibleRows!.last! as! NSIndexPath).row {
// End of loading
let contentSize : CGSize = self.tableView.contentSize
let width = self.tableView.contentSize.width
let height = self.tableView.contentSize.height
}
}
Upvotes: 8
Reputation: 558
Use tableview's content size property to get the height of content in it:
tableView.layoutIfNeeded
let tblSize = tbl_Data.contentSize
print(tbl_Data.contentSize.height)
Upvotes: 2