Marcus
Marcus

Reputation: 9472

Swift: Xcode tableView scroll indefinitely

I have a tableView in swift with ~500 cells. I would like it to scroll on it's own without any user input. Sort of like a stock ticker displaying information. How do I do this? In addition, when it hits the bottom I want it to start at the top again. Is there any methods or tools to do this?

Upvotes: 0

Views: 395

Answers (2)

Alaeddine
Alaeddine

Reputation: 6212

You may use scrollToRowAtIndexPath and combine it with animateWithDuration method of UIView

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

You can also use a timer to scroll each time to the next cell and when you reach the last cell you scroll to top again cell by cell.

var timer = NSTimer.scheduledTimerWithTimeInterval(0.4, target: self, selector: Selector("roll"), userInfo: nil, repeats: true)


func roll() {
    // scroll to next row until we reach the last cell, in that case scroll back until reach the first cell
}

This SO question can help but it's in Objective-C

Upvotes: 2

Jeff Lewis
Jeff Lewis

Reputation: 2866

There is a method you can use: scrollToRowAtIndexPath: atScrollPosition: animated:

Here is a link to the documentation: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableView_Class/index.html#//apple_ref/occ/instm/UITableView/scrollToRowAtIndexPath:atScrollPosition:animated:

Upvotes: 3

Related Questions