2015-03-31 32 views
17

我在表格視圖中添加了一個搜索欄,當用戶搜索時,我希望表格視圖滾動到您鍵入的文本。我如何自動滾動表格視圖? (Swift)

如何自動滾動滾動視圖?

這是我試過的,但我得到一個錯誤,說一個整數不能轉換爲索引路徑。我該怎麼辦?

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

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

回答

26

你要調用的方法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

5

如果你想自動向下滾動:

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

3

這裏的雨燕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) 
9

對於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) 
0

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

斯威夫特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

相關問題