最近自己在写一个APP,其中需要实现搜索框搜索功能,于是乎就想写篇博客介绍下UISearchController和搜索框的实现。
我写的是一个天气预报APP,直接以我APP中的源代码来详细介绍下搜索框的实现。
注:在iOS 8.0以上版本中, 我们可以使用UISearchController来非常方便地在UITableView中添加搜索框. 而在之前版本中, 我们还是必须使用UISearchBar + UISearchDisplayController的组合方式。

初始化UISearchController

使用UISearchController要继承UISearchResultsUpdating协议, 搜索必须实现UISearchResultsUpdating方法.通过UISearchController的active属性来判断输入框是否处于active状态,然后更新UITableview

搜索完之后,将搜索框移除


网友评论