Reputation: 387
I have a UITableView with many cells.
Each cells following it's contents have dynamic height.
But I notice that UITableView sometimes don't scroll the last indexPath when I enter this viewController.
Have any idea to fix it.
tableView.estimatedRowHeight = 200.0
tableView.rowHeight = UITableViewAutomaticDimension
let bottomOffSet = CGPoint(x: 0, y: self.tableView.contentSize.height - self.tableView.bounds.size.height)
self.tableView.setContentOffset(bottomOffSet, animated: false)
PS: I don't want to use "scrollToRow", because it would trigger tableview's function "cellForRowAtIndexPath" many times than "setContentOffset".
tableView.scrollToRow(at: IndexPath(row: contents.count - 1, section: 0), at: .top, animated: animated)
Upvotes: 1
Views: 884
Reputation: 24341
Try setting the tableView's
contentOffset
to the tableView's
contentSize
, i.e.
override func viewDidAppear(_ animated: Bool)
{
super.viewDidAppear(animated)
let bottomOffSet = CGPoint(x: 0, y: self.tableView.contentSize.height - self.tableView.bounds.size.height)
self.tableView.setContentOffset(bottomOffSet, animated: false)
}
Set the contentOffset
in viewDidAppear
.
Also implement the following UITableViewDelegate
methods:
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
{
return UITableViewAutomaticDimension
}
override func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat
{
return 126.0 //Maximum possible cell height
}
Upvotes: 0