2,下面是一个搜索条的使用样例,功能如下:
3,效果图
4,代码如下
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
60
61
62
63
64
65
66
67
68
69
70
|
import
UIKit
class
ViewController
:
UIViewController
,
UISearchBarDelegate
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
UITableViewDataSource
UITableViewDelegate
{
// 引用通过storyboard创建的控件
@IBOutlet
var
searchBar :
UISearchBar
!
tableView :
UITableView
!
// 所有组件
ctrls:[
String
] = [
"Label"
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,
"Button1"
"Button2"
"Switch"
]
// 搜索匹配的结果,Table View使用这个数组作为datasource
ctrlsel:[
] = []
override
func
viewDidLoad() {
super
.viewDidLoad()
// 起始加载全部内容
self
.ctrlsel =
.ctrls
// 注册TableViewCell
.tableView.registerClass(
UITableViewCell
.
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,forCellReuseIdentifier:
"SwiftCell"
)
}
// 返回表格行数(也就是返回控件数)
tableView(tableView:
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.5em!important; margin:0px!important; overflow:visible!important; padding:1px 0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:Consolas,numberOfRowsInSection section:
Int
) ->
{
return
.ctrlsel.count
}
NSIndexPath
)
->
UITableViewCell
{
let
identify:
=
"SwiftCell"
// 同一形式的单元格重复使用,在声明时已注册
cell = tableView.dequeueReusableCellWithIdentifier(identify,forIndexPath: indexPath)
as
UITableViewCell
cell.textLabel?.text =
.ctrlsel[indexPath.row]
return
cell
}
searchBar(searchBar:
!,textDidChange searchText:
!) {
if
searchText ==
""
{
.ctrls
}
.ctrlsel = []
for
ctrl
in
.ctrls {
ctrl.lowercaseString.hasPrefix(searchText) {
.ctrlsel.append(ctrl)
}
}
}
// 刷新Table View显示
.tableView.reloadData()
}
//func searchBarSearchButtonClicked(searchBar: UISearchBar!) {
//searchBar.resignFirstResponder()
//}
didReceiveMemoryWarning() {
.didReceiveMemoryWarning()
// dispose of any resources that can be recreated.
}
}
|
原文出自: www.hangge.com 转载请保留原文链接: http://www.hangge.com/blog/cache/detail_562.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。