ios 如何在表格视图中自动滚动?(迅速)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29378009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 05:26:54  来源:igfitidea点击:

How do i automatically scroll in a table view? (Swift)

iosswiftuitableviewsearch

提问by nachshon f

I added a search bar to my table view, and as the user searches, I want the table view to scroll to the text that your typed.

我在表格视图中添加了一个搜索栏,当用户搜索时,我希望表格视图滚动到您输入的文本。

How do I make the scroll view scroll automatically?

如何使滚动视图自动滚动?

This is what I tried, but I get an error saying that an Integer is not convertible to an index Path. What should I do?

这是我尝试过的,但是我收到一条错误消息,指出整数不可转换为索引路径。我该怎么办?

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

or

或者

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

回答by Victor Sigler

You have to call the method scrollToRowAtIndexPathin the following way :

您必须scrollToRowAtIndexPath通过以下方式调用该方法:

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

The above example assume you have only one section. I hope this help you.

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

For Swift 3:

对于 Swift 3:

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

For Swift 4:

对于 Swift 4:

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

回答by Irshad Qureshi

For Swift 5 ( Xcode 11.2.1 ):

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

For Swift 2:

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

回答by Thiago Arreguy

If you want scroll down automatically:

如果你想自动向下滚动:

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:

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

回答by xke

Here's the Swift 2.0 version to scroll to the last row of multiple sections, based on the code from Thiagoearlier in this thread:

这是 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)

回答by Faris

SWIFT 4

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

回答by Roberto E Fierro Gonzalez

Swift 4:

斯威夫特 4:

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

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