我如何自动滚动表格视图? (Swift)

问题描述:

我在表格视图中添加了一个搜索栏,当用户搜索时,我希望表格视图滚动到您键入的文本。我如何自动滚动表格视图? (Swift)

如何自动滚动滚动视图?

这是我试过的,但我得到一个错误,说一个整数不能转换为索引路径。我该怎么办?

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

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

你要调用的方法scrollToRowAtIndexPath以下列方式:

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

上面的例子假设你只有一个部分。我希望这对你有所帮助。

对于斯威夫特3:

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

当你还没有一个搜索栏,只是一个普通的表:调用它viewDidAppear(),你有一个漂亮的动画, viewDidload也为我工作,但没有动画 – kuzdu 2016-04-26 13:57:08

+0

其工作正常,谢谢合适的答案。 – Gaurav 2017-01-10 11:40:58

如果你想自动向下滚动:

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) 
+0

虽然代码是赞赏的,它应该总是有一个附带的解释。这并不需要很长时间,但它是可以预料的。 – peterh 2015-09-08 18:12:06

+0

感谢您分享代码!我引用它来创建Swift 2.0的更新版本。 – xke 2015-11-03 07:46:34

+0

谢谢,它非常帮助我! – javimuu 2017-02-14 09:46:11

这里的雨燕2.0的版本滚动到多节的最后一排的基础上,从Thiago代码在此线程中:

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) 

对于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) 

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

斯威夫特4:

let indexPath = IndexPath(row: numberOfRowYouWant, section: 0) 

self.verbsTable.scrollToRow(at: indexPath, at: UITableViewScrollPosition.middle, animated: true) 
+0

您应该格式化您的代码。 – Nikolaus 2018-03-04 23:35:31