Home » Swift » How do i automatically scroll in a table view? (Swift)

How do i automatically scroll in a table view? (Swift)

Posted by: admin November 30, 2017 Leave a comment

Questions:

I added a search bar to my table view, and as the user searches, I want the table view to scroll to the text that your typed.

How do I make the scroll view scroll automatically?

This is what I tried, but I get an error saying that an Integer is not convertible to an index Path. What should I do?

self.tableview.scrollToRowAtIndexPath(1, atScrollPosition: UITableViewScrollPosition.Middle, animated: true)

or

self.tableview.scrollToNearestSelectedRowAtScrollPosition(scrollPosition: UITableViewScrollPosition.Middle, animated: true)
Answers:

You have to call the method scrollToRowAtIndexPath in the following way :

var indexPath = NSIndexPath(forRow: numberOfRowYouWant, inSection: 0)
self.tableview.scrollToRowAtIndexPath(indexPath, 
               atScrollPosition: UITableViewScrollPosition.Middle, animated: true)

The above example assume you have only one section. I hope this help you.

For Swift 3:

let indexPath = NSIndexPath(item: numberOfRowYouWant, section: 2)
tableView.scrollToRow(at: indexPath as IndexPath, at: UITableViewScrollPosition.middle, animated: true)

Questions:
Answers:

For Swift 3 ( Xcode 8.1 ):

        let numberOfSections = self.tableView.numberOfSections
        let numberOfRows = self.tableView.numberOfRows(inSection: numberOfSections-1)

        let indexPath = IndexPath(row: numberOfRows-1 , section: numberOfSections-1)
        self.tableView.scrollToRow(at: indexPath, at: UITableViewScrollPosition.middle, animated: true)

Questions:
Answers:

If you want scroll down automatically:

numberOfSections = tableView.numberOfSections()
let numberOfRows = tableView.numberOfRowsInSection(numberOfSections-1)

var indexPath = NSIndexPath(forRow: numberOfRows, inSection: numberOfSections)
self.tableview.scrollToRowAtIndexPath(indexPath, 
               atScrollPosition: UITableViewScrollPosition.Middle, animated: true)

Questions:
Answers:

Here’s the Swift 2.0 version to scroll to the last row of multiple sections, based on the code from Thiago earlier in this thread:

let numberOfSections = self.tableView.numberOfSections
let numberOfRows = self.tableView.numberOfRowsInSection(numberOfSections-1)

print ("scrolling to bottom row \(numberOfRows)")

let indexPath = NSIndexPath(forRow: numberOfRows-1, inSection: numberOfSections-1)
self.tableView.scrollToRowAtIndexPath(indexPath,
                        atScrollPosition: UITableViewScrollPosition.Middle, animated: true)

Questions:
Answers:

SWIFT 4

/* Variables */
  var scrollTimer = Timer()


//  AN AUTOMATIC SCROLL OF THE YOUT IMAGE
scrollTimer = Timer.scheduledTimer(timeInterval: 3.0, target: self, selector: #selector(automaticScroll), userInfo: nil, repeats: true)




// MARK: - SCROLLVIEW DELEGATE: SHOW CURRENT PAGE IN THE PAGE CONTROL
func scrollViewDidScroll(_ scrollView: UIScrollView) {
    let pageWidth = containerScrollView.frame.size.width
    let page = Int(floor((containerScrollView.contentOffset.x * 2 + pageWidth) / (pageWidth * 2)))
    pageControl.currentPage = page

}

// MARK: - AUTOMATIC SCROLL
@objc func automaticScroll() {
    var scroll = containerScrollView.contentOffset.x
    if scroll < CGFloat(view.frame.size.width) * CGFloat(numberOfImages-1) {
        scroll += CGFloat(view.frame.size.width)
        containerScrollView.setContentOffset(CGPoint(x: scroll, y:0), animated: true)
    } else {
        // Stop the timer
        scrollTimer.invalidate()
    }
}