Swift - 使用UISearchController实现带搜索栏的表格

我原来写过一篇文章“ Swift - 带结果列表的搜索条(UISearchDisplayController)的用法 ”,当时是使用 UISearchDisplayController 来实现带有搜索功能的列表,由于 本身就整合了搜索条和表格,所有用起来很方便。

到了iOS8,苹果废除 UISearchDisplayController,建议我们使用 UISearchController配合 UITableView来实现。我们可以把搜索条放在表格头部,或者放在页面顶部,还是很灵活的。下面通过代码演示如何使用 UISearchController实现具有搜索功能的表格。

效果图如下:

代码如下:
(注:这里对ViewController做了类扩展ViewControllerExtensions.swift,把UITableView和UISearchController的代理方法都写在扩展类里,使代码更加简洁)
--- ViewController.swift ---
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import UIKit
class ViewController : UIViewController {
//展示列表
var tableView: UITableView !
//搜索控制器
countrySearchController = UISearchController ()
//原始数据集
let schoolArray = [ "清华大学" , "北京大学" "中国人民大学" "北京交通大学" "北京工业大学" "北京航空航天大学" "北京理工大学" "北京科技大学" "中国政法大学" "中央财经大学" "华北电力大学" "北京体育大学" "上海外国语大学" "复旦大学" "华东师范大学" "上海大学" "河北工业大学" ]
//搜索过滤后的结果集
searchArray:[ String ] = [ ](){
didSet { self .tableView.reloadData()}
}
override func viewDidLoad() {
super .viewDidLoad()
//创建表视图
.tableView = (frame: UIScreen .mainScreen().applicationFrame,
style: UITableViewStyle . Plain )
.tableView!.delegate = self
.tableView!.dataSource = self
//创建一个重用的单元格
.tableView!.registerClass( UITableViewCell . forCellReuseIdentifier: "MyCell" )
.view.addSubview( .tableView!)
//配置搜索控制器
.countrySearchController = ({
controller = (searchResultsController: nil )
controller.searchResultsUpdater = self
controller.hidesNavigationBarDuringPresentation = false
controller.dimsBackgroundDuringPresentation = false
controller.searchBar.searchBarStyle = . Minimal
controller.searchBar.sizeToFit()
.tableView.tableHeaderView = controller.searchBar
return controller
})()
}
viewDidAppear(animated: Bool ) {
.viewDidAppear( true )
.tableView.reloadData()
}
didReceiveMemoryWarning() {
.didReceiveMemoryWarning()
}
}

--- ViewControllerExtensions.swift ---
56
57
58
59
60
Foundation
UIKit
extension UITableViewDataSource
{
tableView(tableView: Int ) -> Int
{
if ( .countrySearchController.active)
{
return .searchArray.count
} else
{
.schoolArray.count
}
}
NSIndexPath )
-> UITableViewCell
{
//为了提供表格显示性能,已创建完成的单元需重复使用
identify: = "MyCell"
//同一形式的单元格重复使用,在声明时已注册
cell = tableView.dequeueReusableCellWithIdentifier(identify,
forIndexPath: indexPath)
.countrySearchController.active)
{
cell.textLabel?.text = .searchArray[indexPath.row]
cell
}
else
{
.schoolArray[indexPath.row]
cell
}
}
}
UITableViewDelegate
{
)
{
tableView.deselectRowAtIndexPath(indexPath,animated: )
}
UISearchResultsUpdating
{
updateSearchResultsForSearchController(searchController: )
{
.searchArray.removeAll(keepCapacity: false )
searchPredicate = NSPredicate (format: "SELF CONTAINS[c] %@" searchController.searchBar.text!)
array = ( .schoolArray as NSArray )
.filteredArrayUsingPredicate(searchPredicate)
.searchArray = array as ! [ ]
}
原文出自: www.hangge.com 转载请保留原文链接: http://www.hangge.com/blog/cache/detail_797.html

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...