Swift - 搜索条(UISearchBar)的用法
来源:互联网 发布:万网中文域名 编辑:程序博客网 时间:2024/06/12 19:56
1,搜索条Options属性还可设置如下功能样式:
2,下面是一个搜索条的使用样例,功能如下:
Shows Search Results Button:勾选后,搜索框右边显示一个圆形向下的按钮,单击会发送特殊事件。
Shows Bookmarks Button:勾选后,搜索框右边会显示一个书本的按钮,单击会发送特殊事件。
Shows Cancel Button:勾选后,搜索框右边会出现一个“Cancel”按钮,单击会发送特殊事件。
Shows Scope Bar:勾选后,会在搜索条下面出现一个分段控制器。
2,下面是一个搜索条的使用样例,功能如下:
(1)在Main.storyboard界面里拖入一个 Search Bar 和一个 Table View,Search Bar放到Table View的页眉位置
(2)初始化或者搜索条为空时,表格显示所有数据
(3)搜索条不为空时,表格实时过滤显示匹配的项目
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
71
72
73
74
75
import
UIKit
class
ViewController
:
UIViewController
,
UISearchBarDelegate
,
UITableViewDataSource
,
UITableViewDelegate
{
// 引用通过storyboard创建的控件
@IBOutlet
var
searchBar :
UISearchBar
!
@IBOutlet
var
tableView :
UITableView
!
// 所有组件
var
ctrls:[
String
] = [
"Label"
,
"Button1"
,
"Button2"
,
"Switch"
]
// 搜索匹配的结果,Table View使用这个数组作为datasource
var
ctrlsel:[
String
] = []
override
func
viewDidLoad() {
super
.viewDidLoad()
// 起始加载全部内容
self
.ctrlsel =
self
.ctrls
//设置代理
self
.searchBar.delegate =
self
self
.tableView.delegate =
self
self
.tableView.dataSource =
self
// 注册TableViewCell
self
.tableView.registerClass(
UITableViewCell
.
self
,
forCellReuseIdentifier:
"SwiftCell"
)
}
// 返回表格行数(也就是返回控件数)
func
tableView(tableView:
UITableView
, numberOfRowsInSection section:
Int
) ->
Int
{
return
self
.ctrlsel.count
}
// 创建各单元显示内容(创建参数indexPath指定的单元)
func
tableView(tableView:
UITableView
, cellForRowAtIndexPath indexPath:
NSIndexPath
)
->
UITableViewCell
{
// 为了提供表格显示性能,已创建完成的单元需重复使用
let
identify:
String
=
"SwiftCell"
// 同一形式的单元格重复使用,在声明时已注册
let
cell = tableView.dequeueReusableCellWithIdentifier(identify,
forIndexPath: indexPath)
as
UITableViewCell
cell.accessoryType =
UITableViewCellAccessoryType
.
DisclosureIndicator
cell.textLabel?.text =
self
.ctrlsel[indexPath.row]
return
cell
}
// 搜索代理UISearchBarDelegate方法,每次改变搜索内容时都会调用
func
searchBar(searchBar:
UISearchBar
, textDidChange searchText:
String
) {
print
(searchText)
// 没有搜索内容时显示全部组件
if
searchText ==
""
{
self
.ctrlsel =
self
.ctrls
}
else
{
// 匹配用户输入内容的前缀(不区分大小写)
self
.ctrlsel = []
for
ctrl
in
self
.ctrls {
if
ctrl.lowercaseString.hasPrefix(searchText.lowercaseString) {
self
.ctrlsel.append(ctrl)
}
}
}
// 刷新Table View显示
self
.tableView.reloadData()
}
// 搜索代理UISearchBarDelegate方法,点击虚拟键盘上的Search按钮时触发
/**func searchBarSearchButtonClicked(searchBar: UISearchBar) {
searchBar.resignFirstResponder()
}**/
override
func
didReceiveMemoryWarning() {
super
.didReceiveMemoryWarning()
}
}
0 0
- Swift - 搜索条(UISearchBar)的用法
- Swift - 搜索条(UISearchBar)的用法
- swift 搜索框(UISearchBar)实现简单的搜索功能
- swift 搜索框(UISearchBar)实现简单的搜索功能(swift3)
- 使用搜索条UISearchBar 谓词实现搜索的判断
- UISearchBar搜索框用法
- 实现UISearchBar空字符串搜索 swift
- swift中UISearchBar的使用
- swift中UISearchBar的使用
- UISearchBar的基本用法
- swift搜索条
- Swift基础(三十)UISearchBar
- 搜索条UISearchBar自定义样式 去掉上下阴影下划线 修改取消按钮的文字、颜色
- UISearchBar搜索
- UISearchBar搜索
- 【Swift】UISearchBar输入框的背景颜色
- iOS UISearchBar使用全解 搜索条使用大全 swift3
- iOS UISearchBar调整搜索框的颜色
- Android混淆配置
- 每周重拾两个Linux命令:cd ------>第一周
- Java容器
- FTP客户端代码解析
- Linux SD卡驱动开发(三) —— SD 卡驱动分析CORE篇
- Swift - 搜索条(UISearchBar)的用法
- java集合类深入分析之Queue篇
- Linux SD卡驱动开发(四) —— SD 控制器之真正的硬件操作
- 上传jar包到私服(仅限于翼支付公司工作流程使用)和涉及的技术总结
- namespace命名空间
- 【mysql学习笔记】-事务处理
- C++之模板
- 13、RedHat6 集群文件系统GFS
- Linux SD卡驱动开发(五) —— SD 卡驱动分析Core补充篇