gpt4 book ai didi

ios - 如何在表格 View 中自动滚动? ( swift )

转载 作者:IT王子 更新时间:2023-10-29 05:13:53 28 4
gpt4 key购买 nike

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

如何让 ScrollView 自动滚动?

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

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)

关于ios - 如何在表格 View 中自动滚动? ( swift ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29378009/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com