4

I'm trying to scroll to the last row of a table view after a user has added a comment to a feed:

self.loadDataFromParse();

// load the last row
let numberOfSections = self.tableView.numberOfSections()
let numberOfRows = self.tableView.numberOfRowsInSection(numberOfSections-1)

if numberOfRows > 0 {
     println(numberOfSections)
     let indexPath = NSIndexPath(forRow: numberOfRows-1, inSection: (numberOfSections-1))
     self.tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: UITableViewScrollPosition.Bottom, animated: true)
}

But tableview only scrolls to about half / 3/4 of the way. Any input appreciated

Victor Sigler
  • 23,243
  • 14
  • 88
  • 105
dancingbush
  • 2,131
  • 5
  • 30
  • 66
  • I would get the `UITableViewCell` of the cell at that indexPath. See if it's the last cell. – Math is Hard Apr 08 '15 at 22:12
  • Not quite sure what you mean, how would you go about this? – dancingbush Apr 08 '15 at 22:15
  • `UITableViewCell cell = self.tableView.cellForRowAtIndexPath(indexPath)` check the information about this cell. See if it's indeed the last cell in your table. Since it only scrolls 3/4 of the way, it seems this is the 3/4th cell right? – Math is Hard Apr 08 '15 at 22:24
  • Thanks, tried that, i get number of rows = 21, but will for the cell: if let cell : TimelineCell = self.tableView.cellForRowAtIndexPath(indexPath)? as? TimelineCell{ println("Last row comments: \(cell.postLabel?.text)"); } ...... i get nil..... – dancingbush Apr 09 '15 at 08:57

1 Answers1

0

This solution may be helpful of others with same issue. This issue is a bug, see discussion here.

I use a footer view for adding comments to a feed so i used its height here and the tableview will scroll to the last row directly above the footer view:

self.tableView.reloadData();

            // load the last row


            let offset = CGPoint(x: 0, y: self.tableView.contentSize.height - self.tableView.frame.size.height + self.FOOTERHEIGHT);

            self.tableView.contentOffset = offset;
Community
  • 1
  • 1
dancingbush
  • 2,131
  • 5
  • 30
  • 66
  • Calling `self.tableView.layoutIfNeeded()` before setting the offset will also do the trick, `reloadData` can get expensive. – bauerMusic Oct 15 '16 at 14:45