[Here’s the short question: Can I easily scroll to the bottom of a TableView? Or, can I use scrollToY so that it scrolls relatively from the current point, rather than from 0? For more explanation, read below.]
I am trying to create a TableView that scrolls to the bottom after I insert a row at the end, something like a typical text messaging app, where the new message I enter will scroll everything up a little bit to make room for the new message, which shows up at the bottom end of the view.
This proved rather tricky, as the only options I could see were to use scrollToIndex (which puts the new message at the top, not at the bottom, and it’s not possible to calculate which message to put at the top because each row is a different height) or to use scrollToY.
I found a field called tableView._view._scrollHeight which helps me do this. Since then I figured out that I could probably get the final position of the last row by checking the onRowRender field, although that’s a bit of a hassle. Anyway, the below code does “work”, except for one thing …
[lua]
local offset = -tableView._view._scrollHeight+tableHeight – tableHeight is the on-screen height of the TableView
tableView:scrollToY({y=offset})
[/lua]
… The problem is that scrollToY seems to always scroll from the very top to the offset point. So, if I have 1000 messages in the TableView, it will scroll through all those 1000 messages or so to get to the point near the bottom, whereas of course I would just want it to scroll up by the few pixels necessary to get to the bottom (normally, it should scroll the height of the new row, but maybe more if the user happens to be elsewhere in the view).
So, how do I scroll just a certain amount, rather than through the whole table? Or is there some easier way to just scroll to bottom in a TableView?