<分区>
有人请帮助我使用 Objective C 在 iOS 8 中搜索表格 View 。 协议(protocol)已更改,searchDisplayController 已被弃用。 我找到了 Apple 的示例代码,但无法理解,所以不要再次粘贴相同的链接。
最佳答案
您使用 UISearchController。要使用它,您首先要使用 initWithSearchResultsController:
对其进行初始化,并向其传递一个用于显示搜索结果的 View Controller 实例。如果你想在同一个 View Controller 中显示你的结果,你也可以传入 nil
而不是 View Controller 。
然后您从它的 searchBar
属性获取搜索 Controller 的搜索栏,并将其添加到您的 View 层次结构中,可能作为 TableView 标题或其他地方。
这就是基本设置的全部内容。如果您点击搜索栏,屏幕会变暗,只要您在其中输入内容,就会显示您传递给初始化程序的搜索结果 Controller 。
为了能够对搜索栏中的更改使用react,您可以设置它的委托(delegate)属性,也可以将搜索 Controller 的 searchResultsUpdater
属性设置为实现 updateSearchResultsForSearchController:
(通过 UISearchResultsUpdating 协议(protocol))。只要搜索栏中的某些内容发生变化,就会调用此方法。通常你会在你的搜索结果 View Controller 中实现它,但你也可以把它放在其他地方。
由于 UISearchController 是 UIViewController 的子类,您还可以像常规 View Controller 一样手动显示它,而无需将其搜索栏添加到不同 View Controller 的 View 中。
所有这些都必须在代码中完成,不能在 Interface Builder 中设置。
关于ios - 有人可以解释如何在 iOS 8 中使用搜索栏。 "searchDisplayController"已弃用。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28796544/