Search code examples
iosiphoneswiftuitableviewuitableviewrowaction

Automatic scroll to the next cell in a PFQueryTableViewController in Swift


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


Solution

  • 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)
        }
    
    }