I have in iOS8 a table view like this:
tableView = UITableView(frame: view.bounds, style: .Plain)
view.addSubview(tableView)
When the user types and sends some text in the keyboard, the application ivoke the following method to scroll the tableView. The goal is to view the new text in the screen (like a chat)
let numberOfRows = tableView.numberOfRowsInSection(0)
if numberOfRows > 0 {
let indexPath = NSIndexPath(forRow: numberOfRows-1, inSection: 0)
tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: UITableViewScrollPosition.Bottom, animated: animated)
}
But the table view does not scroll to the bootom.
Someone has a solution?
Thank you.
Explanation:
Definition of the class:
class ChatViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UITextViewDelegate
then I have the definition of the table view:
var tableView: UITableView!
in viewDidLoad:
tableView = UITableView(frame: view.bounds, style: .Plain)
tableView.dataSource = self
tableView.delegate = self
view.addSubview(tableView)
then I have the call to the code that should make the scroll (second block of code on my answer). When I launch the application I expect the tableview to scroll down, but it does not work.
Call scrollToLastPosition() method after loading the tableview like call from viewWillAppear( ) or after table view reload when some thing changed into tableview
The better solution is to flip upside down the tableView so you can show new message just by
-insertRowsAtIndexPaths:withRowAnimation:
to do it you need first to flip your table by:
then don't forget to flip back your cells, best place to do it in -awakeFromNib by subclassing:
The solution at my problem is:
SWIFT 3 VERSION of @Fox5150 answer, with an extension :
USAGE:
Heres Swift 3 version
The solution below worked for me. It's a combination of solutions found on StackOverflow. I called "
scrollToRowAtIndexPath
" after a very short delay.called by :
Swift 3