The following worked for my Swift 3.1 code using Xcode 8.3.2 focused on iOS 10.1 for iPhone only.
I had the same problem as the OP. My app included a messaging feature. I wanted the last message to be visible when the message view was pushed onto the stack. High table cell heights change due to message content. None of the solutions satisfied me (the last row of the table was not visible or only partially visible). But here is what worked for me:
- In Interface Builder (IB), set the row height of the cell to a value greater than I expected that my average row height will be 50 in my case (be sure to check the "Custom" box).
In the ViewController, where the tableview is implemented inside the viewDidLoad function, set the tableview 'rowHeight' property as follows:
myTableView.rowHeight = UITableViewAutomaticDimension
Also, the function 'viewDidLoad' is set to tableRownHeight tableview for a value higher than you expect the average row height to be. In my case, 140.
Finally, and most importantly, I wrote this bit of code:
extension UITableView { func scrollToBottom() { let rows = self.numberOfRows(inSection: 0) let indexPath = IndexPath(row: rows - 1, section: 0) self.scrollToRow(at: indexPath, at: .top, animated: true) } }
Pay attention to at: .top is what solved my problem. Passing the .bottom value for the UITableViewScrollPosition parameter just didn't work. Even passing .middle worked - but not .bottom .
So, whenever my dataset needs to be updated or a new message arrives, I call
func getTableViewData(){
I checked the results of this “method” by simply changing the at: .bottom to at: .top . Each time I changed it to at: .top , the last rows of the table were completely visible, and the table scroll to the end.
Barns
source share