博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Swift - 搜索条(UISearchBar)的用法
阅读量:7079 次
发布时间:2019-06-28

本文共 5359 字,大约阅读时间需要 17 分钟。

1,搜索条Options属性还可设置如下功能样式:
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
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
        
// 注册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
!) {
        
// 没有搜索内容时显示全部组件
        
if
searchText ==
""
{
            
self
.ctrlsel =
self
.ctrls
        
}
        
else
{
// 匹配用户输入内容的前缀
            
self
.ctrlsel = []
            
for
ctrl
in
self
.ctrls {
                
if
ctrl.lowercaseString.hasPrefix(searchText) {
                    
self
.ctrlsel.append(ctrl)
                
}
            
}
        
}
        
// 刷新Table View显示
        
self
.tableView.reloadData()
    
}
     
    
// 搜索代理UISearchBarDelegate方法,点击虚拟键盘上的Search按钮时触发
    
//func searchBarSearchButtonClicked(searchBar: UISearchBar!) {
        
//searchBar.resignFirstResponder()
    
//}
     
    
override
func
didReceiveMemoryWarning() {
        
super
.didReceiveMemoryWarning()
        
// Dispose of any resources that can be recreated.
    
}
}
--- Main.storyboard ---
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
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
standalone
=
"no"
?>
<
document
type
=
"com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB"
version
=
"3.0"
toolsVersion
=
"6254"
systemVersion
=
"14B25"
targetRuntime
=
"iOS.CocoaTouch"
propertyAccessControl
=
"none"
useAutolayout
=
"YES"
useTraitCollections
=
"YES"
initialViewController
=
"BYZ-38-t0r"
>
    
<
dependencies
>
        
<
plugIn
identifier
=
"com.apple.InterfaceBuilder.IBCocoaTouchPlugin"
version
=
"6247"
/>
    
</
dependencies
>
    
<
scenes
>
        
<!--View Controller-->
        
<
scene
sceneID
=
"tne-QT-ifu"
>
            
<
objects
>
                
<
viewController
id
=
"BYZ-38-t0r"
customClass
=
"ViewController"
customModule
=
"SwiftInAction_008_012"
customModuleProvider
=
"target"
sceneMemberID
=
"viewController"
>
                    
<
layoutGuides
>
                        
<
viewControllerLayoutGuide
type
=
"top"
id
=
"y3c-jy-aDJ"
/>
                        
<
viewControllerLayoutGuide
type
=
"bottom"
id
=
"wfy-db-euE"
/>
                    
</
layoutGuides
>
                    
<
view
key
=
"view"
contentMode
=
"scaleToFill"
id
=
"8bC-Xf-vdC"
>
                        
<
rect
key
=
"frame"
x
=
"0.0"
y
=
"0.0"
width
=
"600"
height
=
"600"
/>
                        
<
autoresizingMask
key
=
"autoresizingMask"
widthSizable
=
"YES"
heightSizable
=
"YES"
/>
                        
<
subviews
>
                            
<
tableView
clipsSubviews
=
"YES"
contentMode
=
"scaleToFill"
fixedFrame
=
"YES"
alwaysBounceVertical
=
"YES"
dataMode
=
"prototypes"
style
=
"plain"
separatorStyle
=
"default"
rowHeight
=
"44"
sectionHeaderHeight
=
"22"
sectionFooterHeight
=
"22"
translatesAutoresizingMaskIntoConstraints
=
"NO"
id
=
"eUR-Ky-A8I"
>
                                
<
rect
key
=
"frame"
x
=
"6"
y
=
"32"
width
=
"320"
height
=
"440"
/>
                                
<
color
key
=
"backgroundColor"
white
=
"1"
alpha
=
"1"
colorSpace
=
"calibratedWhite"
/>
                                
<
searchBar
key
=
"tableHeaderView"
contentMode
=
"redraw"
id
=
"8pv-hH-OQ9"
>
                                    
<
rect
key
=
"frame"
x
=
"80"
y
=
"218"
width
=
"320"
height
=
"44"
/>
                                    
<
autoresizingMask
key
=
"autoresizingMask"
flexibleMaxX
=
"YES"
flexibleMaxY
=
"YES"
/>
                                    
<
textInputTraits
key
=
"textInputTraits"
/>
                                    
<
connections
>
                                        
<
outlet
property
=
"delegate"
destination
=
"BYZ-38-t0r"
id
=
"0l9-UU-iHJ"
/>
                                    
</
connections
>
                                
</
searchBar
>
                                
<
connections
>
                                    
<
outlet
property
=
"dataSource"
destination
=
"BYZ-38-t0r"
id
=
"o1t-B2-xHp"
/>
                                    
<
outlet
property
=
"delegate"
destination
=
"BYZ-38-t0r"
id
=
"p1t-kn-J9Q"
/>
                                
</
connections
>
                            
</
tableView
>
                        
</
subviews
>
                        
<
color
key
=
"backgroundColor"
white
=
"1"
alpha
=
"1"
colorSpace
=
"custom"
customColorSpace
=
"calibratedWhite"
/>
                    
</
view
>
                    
<
connections
>
                        
<
outlet
property
=
"searchBar"
destination
=
"8pv-hH-OQ9"
id
=
"B0M-ya-PE5"
/>
                        
<
outlet
property
=
"tableView"
destination
=
"eUR-Ky-A8I"
id
=
"YCI-P6-0gY"
/>
                    
</
connections
>
                
</
viewController
>
                
<
placeholder
placeholderIdentifier
=
"IBFirstResponder"
id
=
"dkx-z0-nzr"
sceneMemberID
=
"firstResponder"
/>
            
</
objects
>
        
</
scene
>
    
</
scenes
>
</
document
>

上一篇

转载地址:http://hspml.baihongyu.com/

你可能感兴趣的文章
基于 Vue.js 的移动端组件库mint-ui实现无限滚动加载更多
查看>>
Matrix Computations 1
查看>>
springboot上传代码到gitlab并发布上线操作
查看>>
FILE * fopen(const char * path,const char * mode);
查看>>
[Flask]sqlalchemy使用count()函数遇到的问题
查看>>
[python](Docker SDK)上传镜像到私有仓库(tls、身份认证)
查看>>
听说是阿里笔试题
查看>>
使用pm2管理nodejs应用
查看>>
MySQL基础之---mysqlimport工具和LOAD DATA命令导入文本文件
查看>>
php 读取文件头部两个字节 判断文件的实际类型
查看>>
异或交换真的比开一个tmp快吗?
查看>>
使用sea.js管理你项目js文件
查看>>
windows device driver 小结感想
查看>>
SQLServer获取临时表列名并判断指定列名是否存在
查看>>
4827 妹子[快速乘法]
查看>>
Ubuntu的一些使用记录
查看>>
DataBase Connection Failed的一点解决办法(PHP项目)
查看>>
SilverLight控件之ContextMenu和RadContextMenu(菜单)
查看>>
css3背景颜色渐变属性 兼容性测试基础环境为:windows系统;IE6.0+, Firefox4.0+, Chrome4.0+, Safari4.0+, Opera15.0+...
查看>>
word怎么删除空白页
查看>>