How do I start a UITableView
scrolled to the last cell?
just have a plain UITableView(frame: CGRectZero, style: UITableViewStyle.Plain)
that when presented on screen, will start scrolled all the way to the bottom.
I've tried:
// 1
reloadData()
scrollToRowAtIndexPath(
NSIndexPath(forItem: dataArray.count-1, inSection: 0),
atScrollPosition: .Top, animated: false
)
// 2
reloadData()
var contentOffset = self.contentOffset
contentOffset.y = CGFloat.max
setContentOffset(contentOffset, animated: false)
on the tableView's init
method (in my subclass)
I've also tried the classic CGAffineTransformMakeScale(1,-1)
hack, but that makes my cells stick to the bottom and I want them stuck the the top (but scrolled to the bottom). (that is only relevant if I have few cells, when they don't fill the entire UITableView
space)
EDIT: another detail, I'm using dynamic cells UITableViewAutomaticDimension
.
To scroll to the bottom of the table view items, use ScrollToRow. Three parameters are required for this: NSIndexPath: Specify which row item to scroll to.
UITableView is a subclass of UIScrollView that allows users to scroll the table vertically (the closely-related UICollectionView class allows for horizontal scrolling and complex two-dimensional layouts).
Index paths describe an item's position inside a table view or collection view, storing both its section and its position inside that section.
Programmatic UITableView Scrolling. Scrolling to a certain point in a table view is a common requirement. For example, you might scroll to show new entries after loading new data or to show that a row has updated data. Since UITableView inherits from UIScrollView it’s easy to scroll programmatically.
self.tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: .Top, animated: true) There are 2 options (in addition to the index path): atScrollPosition:lets us choose where the row should be displayed when we scroll. We can opt to scroll so that the row is at the top of the view, the bottom, or the middle.
We can opt to scroll so that the row is at the top of the view, the bottom, or the middle. There’s also an option called None which scrolls as little as possible to display the row. So if you’re below the row it’ll scroll it to the bottom of the screen but if you’re above the row it’ll scroll it to the top.
In a UITableView, you can quickly reload, insert, delete, and reorder rows. All these actions come with standard animations out of the box. In a scroll view, you have to write a ton of code to get the same functionality.
This will scroll to bottom without any glitch, but if you use Tableview scroll-to-row property then there will be glitch.
For Swift 3 use
self.TableView.reloadData() // To populate your tableview first
//Since we have to wait until the table is reload
DispatchQueue.main.async {
let bottomOffset = CGPoint(x: 0, y: self.TableView.contentSize.height - self.TableView.frame.size.height)
self.TableView.setContentOffset(bottomOffset, animated: false)
}
For Objective C use
[YourTableView reloadData]; // To populate your tableview first
[YourTableView setContentOffset:CGPointMake(0, YourTableView.contentSize.height - YourTableView.frame.size.height)];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With