我原来写过一篇文章“Swift - 带结果列表的搜索条(UISearchDisplayController)的用法”,当时是使用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
|
import
UIKit
class
ViewController
:
UIViewController
{
//展示列表
var
tableView:
UITableView
!
//搜索控制器
var
countrySearchController =
UISearchController
()
//原始数据集
let
schoolArray = [
"清华大学"
,
"北京大学"
,
"中国人民大学"
,
"北京交通大学"
,
"北京工业大学"
,
"北京航空航天大学"
,
"北京理工大学"
,
"北京科技大学"
,
"中国政法大学"
,
"中央财经大学"
,
"华北电力大学"
,
"北京体育大学"
,
"上海外国语大学"
,
"复旦大学"
,
"华东师范大学"
,
"上海大学"
,
"河北工业大学"
]
//搜索过滤后的结果集
var
searchArray:[
String
] = [
String
](){
didSet
{
self
.tableView.reloadData()}
}
override
func
viewDidLoad() {
super
.viewDidLoad()
//创建表视图
self
.tableView =
UITableView
(frame:
UIScreen
.mainScreen().applicationFrame,
style:
UITableViewStyle
.
Plain
)
self
.tableView!.delegate =
self
self
.tableView!.dataSource =
self
//创建一个重用的单元格
self
.tableView!.registerClass(
UITableViewCell
.
self
, forCellReuseIdentifier:
"SwiftCell"
)
self
.view.addSubview(
self
.tableView!)
//配置搜索控制器
self
.countrySearchController = ({
let
controller =
UISearchController
(searchResultsController:
nil
)
controller.searchResultsUpdater =
self
controller.hidesNavigationBarDuringPresentation =
false
controller.dimsBackgroundDuringPresentation =
false
controller.searchBar.searchBarStyle = .
Minimal
controller.searchBar.sizeToFit()
self
.tableView.tableHeaderView = controller.searchBar
return
controller
})()
}
override
func
viewDidAppear(animated:
Bool
) {
super
.viewDidAppear(
true
)
self
.tableView.reloadData()
}
override
func
didReceiveMemoryWarning() {
super
.didReceiveMemoryWarning()
}
}
|
--- ViewControllerExtensions.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
57
58
59
|
import
Foundation
import
UIKit
extension
ViewController
:
UITableViewDataSource
{
func
tableView(tableView:
UITableView
, numberOfRowsInSection section:
Int
) ->
Int
{
if
(
self
.countrySearchController.active)
{
return
self
.searchArray.count
}
else
{
return
self
.schoolArray.count
}
}
func
tableView(tableView:
UITableView
, cellForRowAtIndexPath indexPath:
NSIndexPath
)
->
UITableViewCell
{
//为了提供表格显示性能,已创建完成的单元需重复使用
let
identify:
String
=
"SwiftCell"
//同一形式的单元格重复使用,在声明时已注册
let
cell = tableView.dequeueReusableCellWithIdentifier(identify, forIndexPath: indexPath)
as
!
UITableViewCell
if
(
self
.countrySearchController.active)
{
cell.textLabel?.text =
self
.searchArray[indexPath.row]
return
cell
}
else
{
cell.textLabel?.text =
self
.schoolArray[indexPath.row]
return
cell
}
}
}
extension
ViewController
:
UITableViewDelegate
{
func
tableView(tableView:
UITableView
, didSelectRowAtIndexPath indexPath:
NSIndexPath
)
{
tableView.deselectRowAtIndexPath(indexPath, animated:
true
)
}
}
extension
ViewController
:
UISearchResultsUpdating
{
func
updateSearchResultsForSearchController(searchController:
UISearchController
)
{
self
.searchArray.removeAll(keepCapacity:
false
)
let
searchPredicate =
NSPredicate
(format:
"SELF CONTAINS[c] %@"
,
searchController.searchBar.text)
let
array = (
self
.schoolArray
as
NSArray
).filteredArrayUsingPredicate(searchPredicate)
self
.searchArray = array
as
! [
String
]
}
}
|