0

I have a PFTableViewController with PFTableViewCells in Swift.

In each TableViewCell (with a height of 80% the screen size), there is a button. I would like that when a user select the button, there is an automatic scroll to the next TableViewCell.

Any idea how I can make it?

Thanks a lot

skifen128
  • 35
  • 3

1 Answers1

0

Just determine the indexPath and scroll to the next indexPath. This example assumes a flat table without sections.

func buttonTapped(sender: UIButton) {
    let point = sender.convertPoint(CGPointZero, toView:tableView)
    let indexPath = tableView.indexPathForRowAtPoint(point)!
    // check for last item in table
    if indexPath.row < tableView.numberOfRowsInSection(indexPath.section) {
        let newIndexPath = NSIndexPath(forRow:indexPath.row + 1 inSection:indexPath.section)
        tableView.scrollToRowAtIndexPath(
             newIndexPath, atScrollPosition:.Top, animated: true)
    }

}
Mundi
  • 79,884
  • 17
  • 117
  • 140
  • awsome! However how can I call the tableView.indexPath since I am in my TableViewCell and it doesn't know tableView? – skifen128 Jul 25 '15 at 11:09
  • that's because my buttonTapped method is in my TableViewCell class – skifen128 Jul 25 '15 at 11:12
  • It belongs to the *controller*, not the cell. The cell is a *view* and should not manipulate other views (in this case, the table view) managed by the controller. – Mundi Jul 25 '15 at 11:13
  • Ok thanks. I added : var tableView = self.superview let point = sender.convertPoint(CGRectZero, toView:tableView) Still give me the error: "Cannot Invoke 'convertPoint' with an argument of type "(CGRect, ToView: ?UIView' – skifen128 Jul 25 '15 at 11:18
  • Do you have any idea how to fix that? – skifen128 Jul 25 '15 at 11:19
  • Put the code into the controller (!!). Also, typo: `CGPointZero` is correct. – Mundi Jul 25 '15 at 11:55
  • Hi! Thank you, it's working, however the code doesn't check for the last item in table -> app crashes at the end... Do you know why? – skifen128 Jul 27 '15 at 07:56