如何自动在表格视图中滚动?(迅速) [英] How do i automatically scroll in a table view? (Swift)

查看:83
本文介绍了如何自动在表格视图中滚动?(迅速)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

这是我尝试过的方法,但是我收到一条错误消息,说Integer不能转换为索引Path.我该怎么办?

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)

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

推荐答案

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

You have to call the method scrollToRowAtIndexPath in the following way :

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.

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

这篇关于如何自动在表格视图中滚动?(迅速)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆