如何在 UITableView 中实现 UISearchController - Swift [英] How to implement UISearchController in UITableView - Swift

查看:30
本文介绍了如何在 UITableView 中实现 UISearchController - Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Swift 的新手,我正在尝试向我的 UITableView 添加搜索功能,该功能位于 UIViewController 类中.我用谷歌搜索了很多,发现 UISearchDisplayController 已被弃用并被 UISearchController 取代.当我尝试搜索教程时,我没有找到任何专门针对 UITableView 的教程.其中一些用于在 UITableViewController 中实现.所以这是我的问题:

I am a newbie to Swift and I am trying to add search functionality to my UITableView which is in a UIViewController class. I googled a lot and found that UISearchDisplayController has been deprecated and replaced by UISearchController. When I tried to search for tutorials I did not find any specifically for UITableView. Some of them were for implementing in UITableViewController. So here are my questions:

  1. 我们可以在 UIViewController 类中的 UITableView 中实现 UISearchController 吗?(我想我们可以)

  1. Can we implement UISearchController in a UITableView that is in a UIViewController class? (I guess we can)

如果可以,请转换这些用 Objective-C 编写的代码行.或者如果有一些非常好的教程,请告诉我.

If we can, please convert these line of codes written in Objective-C. Or if there are some really good tutorials please let me know.

@property (strong, nonatomic) UISearchController *searchController;

UITableViewController *searchResultsController = [[UITableViewController alloc] init];

// Init UISearchController with the search results controller
 self.searchController = [[UISearchController alloc] initWithSearchResultsController:searchResultsController];

// Link the search controller
self.searchController.searchResultsUpdater = self;

来源

我正在尝试将搜索结果更新程序指定为

EDIT : I am trying to assign the search results updater in as

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UISearchResultsUpdating {

    override func viewDidLoad() {

       super.viewDidLoad()
       self.view.backgroundColor = UIColor.blackColor() 
       self.addTableViewWithSection() 
       self.searchResultsController = UITableViewController()           
       var controller = UISearchController(searchResultsController: nil)            
       controller.searchResultsUpdater = self           
       controller.dimsBackgroundDuringPresentation = false   
    }
}

*但是,在 controller.searchResultsUpdater = self 线上,我得到的错误是无法将类型ViewController"的值分配给UISearchResultsUpdating"类型的值?.. 所以我真的怀疑我是否可以在 UIViewController 类型类中使用 UISearchController.

*However on the line controller.searchResultsUpdater = self i get the eroor as Cannot assign a value of typ "ViewController" to a value of type "UISearchResultsUpdating?" .. So i really doubt if i can use UISearchController in UIViewController type class.

推荐答案

是的,搜索的工作方式已经彻底改变,我认为这是一个更好的系统.对于大多数简单的实现,新系统要好得多,而且直截了当.使用它非常简单.

Yes, the way search works has been radically changed for what I consider to be a better system. The new system is much better and straight to the point for most simple implementations. Using it is pretty straightforward.

首先,让你的类遵守 UISearchResultsUpdating 协议.

First, make your class comply with the UISearchResultsUpdating protocol.

class MyTableViewController: UITableViewController, UISearchResultsUpdating {}

将其添加到搜索控制器属性:

Add it the search controller property:

class MyTableViewController: UTableViewController, UISearchResultsUpdating {
    let searchController = UISearchController(searchResultsController: nil)
}

在viewDidLoad中添加搜索栏:

Add the search bar in viewDidLoad:

override func viewDidLoad() {
    super.viewDidLoad()

    searchController.searchResultsUpdater = self
    searchController.hidesNavigationBarDuringPresentation = false
    searchController.dimsBackgroundDuringPresentation = false
    searchController.searchBar.sizeToFit()
    self.tableView.tableHeaderView = searchController.searchBar
}

最后,实现来自 UISearchResultsUpdating 协议的 updateSearchResults:for 方法:

And finally, implement the updateSearchResults:for method that comes from the UISearchResultsUpdating protocol:

func updateSearchResults(for searchController: UISearchController) {

}

此方法的实现当然取决于您从何处搜索数据.它将在您键入搜索内容时更新您当前的表视图.确保更新数据源并在 updateSearchResultsForSearchController 方法中重新加载表视图.同样,它会在您键入时更新,因此请确保如果您要搜索的数据很大或者您是从 Internet 搜索,请在此方法中添加一些并发性.

Your implementation of this method will of course depend on where you are searching the data from. It will update your current table view with the contents of your search as you type them. Make sure you update your data source and reload the table view in the updateSearchResultsForSearchController method. Again, it updates as you type so make sure that if your data to search in is big or if you are searching from the internet, add some concurrency in this method.

如果您想使用不同的控制器来填充搜索结果,您可以在初始化 searchController 属性时传递该 VC.

If you want to use a different controller to populate your search results with, you can pass that VC when initialising the searchController property.

编辑:我已重读您的问题,但我好像忘记添加一些重要内容.

EDIT: I have reread your question and it looks like I forgot to add something important.

UITableViewController 有一个名为 tableView 的成员.您可以获取控制器的表视图,但要填充您的 UITableView,您不需要触摸它.您不能在 UITableView 中直接使用 SearchController 逻辑.你必须与控制器合作才能实现它.使用 UITableViewController 委托和数据源方法根据搜索结果更新表格 UI.

UITableViewController has a member called tableView. You can grab the controller's table view , but to populate your UITableView, you don't need to touch it. You can't use the SearchController logic directly with UITableView. You gotta work with the controller to get it there. Use the UITableViewController delegate and data source methods to get your table UI updated with your search results.

请继续阅读如何使用 UITableViewController、UITableViewDelegate 和 UITableViewDataSource,以便您了解如何在此处获取搜索结果.

Please read on using UITableViewController, UITableViewDelegate, and UITableViewDataSource so you can understand how to get your search results there.

这篇关于如何在 UITableView 中实现 UISearchController - Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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