Load table view from bottom, scroll up (reverse table) (iOS)

I would like to load my desktop backwards, which means the tableView is loaded from the bottom and scrolled up to see more content.

I first tried changing the dataSource array. The content is reversed, but it still loads from the top and the user has to scroll down to see more content.

Then I tried to load the tableView from below in viewWillAppear:

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    if (self.conversationTableView.contentSize.height > self.conversationTableView.frame.size.height) {
        let offset = CGPoint(x: CGFloat(0), y: CGFloat(self.conversationTableView.contentSize.height - self.conversationTableView.frame.size.height))
        self.conversationTableView.setContentOffset(offset, animated: true)
    }
}

      

This attempt however does not work, since I am loading images asynchronously (I am using NSTextAttachment which populates my UITextView in every cell). Every time my placeholder is replaced with a real loaded image, it causes the content of the tableView to shift (the height of the placeholder image is almost always less than the height of the loaded image). Therefore setting contentOffset this way does not work.

How do I load the tableView from the bottom and scroll up to see more content?

+3


source to share


2 answers


The best way is to flip the tableView and its cells. This way, if the cell were to be resized (due to things like asynchronous loading), you still load from the bottom of the table and not into the offset.

//In ViewDidLoad
conversationTableView.transform = CGAffineTransform(rotationAngle: -(CGFloat)(M_PI));

//In cellForRowAtIndexPath
cell.transform = CGAffineTransform(rotationAngle: CGFloat(M_PI));

      

Also: if you have a headerView, just set it to tableView.tableFooterView

:

var tableHeaderView = UIView(frame: headerFrame)
tableHeaderView.transform = CGAffineTransform(rotationAngle: CGFloat(M_PI));
conversationTableView.tableFooterView = tableHeaderView

      



And if you have footerView and headerView, just set header to footer and footer to header.

Edit: If you want to flip the scroll indicator:

conversationTableView.scrollIndicatorInsets = UIEdgeInsetsMake(0.0, 0.0, 0.0, conversationTableView.bounds.size.width - 8.0)

      

+16


source


In your method, viewWillAppear

do the following:

tableView.scrollToRowAtIndexPath(bottomIndexPath, atScrollPosition: .Bottom,
      animated: true)

      



Where bottomIndexPath

is the indexPath for the last row in your table view. If you have an array of length n

and only one section in your table, this usually will IndexPath(item: n-1, section: 0)

, but be sure to check your specific one UITableView

.

This will get you started at the bottom of the tableView. Having done this, all you have to do is make sure that your method cellForRowAtIndexPath

on yours UITableViewDataSource

returns the data in reverse order and you get the desired effect.

+1


source







All Articles