UIViewController 中的 UISearchController [英] UISearchController in a UIViewController

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

问题描述

我希望在 Swift 中创建与 Apple 地图应用程序类似的功能.无论如何将 UISearchController 集成到常规视图中(即:不是 UITableView).在连接的搜索栏内单击后,通过 Storyboard 放入一个会导致崩溃.或者有什么方法可以用 UITableView 实现这个结果?

I'm looking to create similar functionality to Apple's maps application in Swift. Is there anyway to integrate a UISearchController in to a regular view (i.e.: not a UITableView). Dropping one in through Storyboard results in a crash after clicking inside the connected searchbar. Or is there some way I can achieve this outcome with a UITableView?

推荐答案

我在故事板的视图控制器中添加了一个搜索栏和搜索显示控制器.视图控制器仅包含搜索栏和搜索显示控制器,并没有自己的 TableView.当您在视图控制器中添加搜索栏时,它会自动将您的视图控制器设置为委托.

I added a Search Bar and Search Display Controller in my View Controller in the storyboard. The view controller contains only the search bar and search display controller and does not have it's own TableView. When you add the search bar in your view controller, it sets your view controller as it's delegate automatically.

现在搜索栏和搜索显示控制器有一个自己的表格视图,当您在框内单击并开始输入时,它会使用它来显示搜索结果.这个表视图需要你的视图控制器提供 numberOfRowsInSection 和 cellForRowAtIndexPath 函数的实现,以便它正确显示数据.

Now the Search Bar and Search Display Controller has a table view of itself which it uses to display the search results when you click inside the box and start typing. This table view expects your view controller to provide the implementations of the numberOfRowsInSection and cellForRowAtIndexPath functions for it to display the data properly.

当你在没有这些的情况下运行你的项目并点击搜索栏内部时,你会得到以下错误:-

When you run your project without these and tap inside the search bar, you will get the following error:-

tableView:numberOfRowsInSection:]: 无法识别的选择器发送到实例 0x7fbf63449660*** 由于未捕获的异常NSInvalidArgumentException"而终止应用

tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x7fbf63449660 *** Terminating app due to uncaught exception 'NSInvalidArgumentException'

如果您看到,错误出在 numberOfRowsInSection 方法上.

If you see, the error is at the numberOfRowsInSection method.

更改您的视图控制器定义

Change your view controller definition from

class ViewController: UIViewController

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource

并实现所需的方法,它们是:-

and implement the required methods which are:-

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    return UITableViewCell()
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 0
}

我刚刚在上述方法中添加了默认返回值.

I have just added default return values in the above methods.

现在,如果您在 searchviewdelegate 方法中过滤掉数据源并在上述两种方法中正确设置行数和单元格信息,它应该可以工作.

Now if you filter out your data source in your searchviewdelegate methods and set up your number of rows and cell info in the above two methods properly, it should work.

希望这会有所帮助!

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

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