我如何在 table 视图中自动滚动? (Swift)
How do i automatically scroll in a table view? (Swift)
我在 table 视图中添加了一个搜索栏,当用户搜索时,我希望 table 视图滚动到您键入的文本。
如何让滚动视图自动滚动?
这是我尝试过的方法,但我收到一条错误消息,指出整数无法转换为索引路径。我该怎么办?
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)
以上示例假设您只有一个部分。希望对你有帮助。
对于Swift 3:
let indexPath = NSIndexPath(item: numberOfRowYouWant, section: 2)
tableView.scrollToRow(at: indexPath as IndexPath, at: UITableViewScrollPosition.middle, animated: true)
对于Swift 4:
let indexPath = NSIndexPath(item: numberOfRowYouWant, section: 2)
tableView.scrollToRow(at: indexPath as IndexPath, at: UITableView.ScrollPosition.middle, animated: true)
如果你想自动向下滚动:
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)
Swift 4:
let numberOfSections = tableView.numberOfSections
let numberOfRows = tableView.numberOfRows(inSection: numberOfSections - 1)
let indexPath = NSIndexPath(row: numberOfRows, section: numberOfSections)
self.tableView.scrollToRow(at: indexPath as IndexPath,
at: UITableView.ScrollPosition.middle, animated: true)
这里是 Swift 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 5 ( Xcode 11.2.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: .middle, animated: true)
对于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)
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()
}
}
Swift 4:
let indexPath = IndexPath(row: numberOfRowYouWant, section: 0)
self.verbsTable.scrollToRow(at: indexPath, at: UITableViewScrollPosition.middle, animated: true)
我在 table 视图中添加了一个搜索栏,当用户搜索时,我希望 table 视图滚动到您键入的文本。
如何让滚动视图自动滚动?
这是我尝试过的方法,但我收到一条错误消息,指出整数无法转换为索引路径。我该怎么办?
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)
以上示例假设您只有一个部分。希望对你有帮助。
对于Swift 3:
let indexPath = NSIndexPath(item: numberOfRowYouWant, section: 2)
tableView.scrollToRow(at: indexPath as IndexPath, at: UITableViewScrollPosition.middle, animated: true)
对于Swift 4:
let indexPath = NSIndexPath(item: numberOfRowYouWant, section: 2)
tableView.scrollToRow(at: indexPath as IndexPath, at: UITableView.ScrollPosition.middle, animated: true)
如果你想自动向下滚动:
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)
Swift 4:
let numberOfSections = tableView.numberOfSections
let numberOfRows = tableView.numberOfRows(inSection: numberOfSections - 1)
let indexPath = NSIndexPath(row: numberOfRows, section: numberOfSections)
self.tableView.scrollToRow(at: indexPath as IndexPath,
at: UITableView.ScrollPosition.middle, animated: true)
这里是 Swift 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 5 ( Xcode 11.2.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: .middle, animated: true)
对于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)
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()
}
}
Swift 4:
let indexPath = IndexPath(row: numberOfRowYouWant, section: 0)
self.verbsTable.scrollToRow(at: indexPath, at: UITableViewScrollPosition.middle, animated: true)