nac*_*n f 18 search uitableview ios swift
我在表格视图中添加了一个搜索栏,当用户搜索时,我希望表格视图滚动到您键入的文本.
如何使滚动视图自动滚动?
这是我尝试过的,但是我得到一个错误,说Integer不能转换为索引Path.我该怎么办?
self.tableview.scrollToRowAtIndexPath(1, atScrollPosition: UITableViewScrollPosition.Middle, animated: true)
Run Code Online (Sandbox Code Playgroud)
要么
self.tableview.scrollToNearestSelectedRowAtScrollPosition(scrollPosition: UITableViewScrollPosition.Middle, animated: true)
Run Code Online (Sandbox Code Playgroud)
Vic*_*ler 30
您必须以scrollToRowAtIndexPath
下列方式调用该方法:
var indexPath = NSIndexPath(forRow: numberOfRowYouWant, inSection: 0)
self.tableview.scrollToRowAtIndexPath(indexPath,
atScrollPosition: UITableViewScrollPosition.Middle, animated: true)
Run Code Online (Sandbox Code Playgroud)
上面的例子假设你只有一个部分.我希望这对你有帮助.
对于Swift 3:
let indexPath = NSIndexPath(item: numberOfRowYouWant, section: 2)
tableView.scrollToRow(at: indexPath as IndexPath, at: UITableViewScrollPosition.middle, animated: true)
Run Code Online (Sandbox Code Playgroud)
对于Swift 4.2(Xcode 12.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: UITableView.ScrollPosition.middle, animated: true)
Run Code Online (Sandbox Code Playgroud)
对于Swift 2:
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)
Run Code Online (Sandbox Code Playgroud)
如果要自动向下滚动:
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)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
25601 次 |
最近记录: |