Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start UITableView scrolled to bottom

How do I start a UITableView scrolled to the last cell?

  • not animated
  • not after view appeared
  • not after table view is even added as a subview

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.

like image 670
Rodrigo Ruiz Avatar asked Aug 28 '16 22:08

Rodrigo Ruiz


People also ask

How do I scroll to the bottom of UITableView?

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.

Is UITableView scrollable?

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).

What is Indexpath in tableView Swift?

Index paths describe an item's position inside a table view or collection view, storing both its section and its position inside that section.

Can You scroll in UITableView programmatically?

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.

How do I scroll to the bottom of a Tableview?

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.

How do I scroll a row in a viewport?

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.

What is the difference between UITableView and ScrollView in Salesforce?

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.


1 Answers

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)];
like image 112
Irfan Anwar Avatar answered Oct 26 '22 01:10

Irfan Anwar