简体   繁体   中英

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?

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 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM