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

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

问题描述

我是Swift的新手,我正在尝试将搜索功能添加到我的 UITableView ,它位于 UIViewController 上课。我google了很多,发现 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. 我们可以在一个实现 UISearchController UITableView 是否在 UIViewController 类中? (我想我们可以)

  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;


Source

编辑:
我正在尝试将搜索结果更新程序分配为

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   
    }
}

*但是在行控制器上.searchResultsUpdater = self我得到eroor as无法将typViewController的值赋值为 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 {}

添加搜索controller属性:

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
}

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

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

func updateSearchResults(for searchController: UISearchController) {

}

您对此方法的实现当然取决于您从哪里搜索数据。它会在您键入时使用搜索内容更新当前表格视图。确保更新数据源并在 updateSearchResultsForSearchController 方法中重新加载表视图。同样,它会在您键入时更新,因此请确保如果您要搜索的数据很大或者您是从互联网上搜索,请在此方法中添加一些并发。

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有一个名为<的成员code>的tableView 。您可以获取控制器的表视图,但要填充 UITableView ,您无需触摸它。您不能将SearchController逻辑 直接 与UITableView一起使用。你必须与控制器一起工作才能实现它。使用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天全站免登陆