Swift - 搜索条(UISearchBar)的用法
![](http://www.hangge.com/blog_uploads/201504/2015041714454970930.png)
![](http://www.hangge.com/blog_uploads/201501/2015012916131310737.png)
![](http://www.hangge.com/blog_uploads/201501/2015012916132519867.png)
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 > |
上一篇Swift - 将表格UITableView滚动条移动到底部
Swift - 搜索条(UISearchBar)的用法的更多相关文章
- iOS 搜索条使用详解
在ios开发中搜索条的使用挺常见的,不过之前一直没用到也没细细研究,最近做外包项目的时候刚好用到,在这里记录一下使用的过程,只要理解了原理,其实还是比较简单的!上传的图片有点大,刚好可以看清楚它的使用 ...
- UI UISearchBar UISearchDisplayController实现搜索条、解析颜色
本文转载至 http://blog.sina.com.cn/s/blog_bf2d33bd01017q6l.html @interface ThirdViewController : UIViewCo ...
- 使用CSS3和jQuery可伸缩的搜索条
使用CSS3和jQuery可伸缩的搜索条 helloweba.com 作者:月光光 时间:2013-12-09 21:23 标签: CSS3 jquery 搜索条在我们网站是必不可少的,尤其是在有限的 ...
- 模仿京东顶部搜索条效果制作的一个小demo
最近模仿京东顶部搜索条效果制作的一个小demo,特贴到这里,今后如果有用到可以参考一下,代码如下 #define kScreenWidth [UIScreen mainScreen].bounds.s ...
- 一个漂亮的DIV搜索条
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 【转】 Pro Android学习笔记(五十):ActionBar(3):搜索条
目录(?)[-] ActionBar中的搜索条 通过Menu item上定义search view 进行Searchable的配置 在activity中将search view关联searchable ...
- 20个Flutter实例视频教程-第10节: 一个不简单的搜索条-1
20个Flutter实例视频教程-第10节: 一个不简单的搜索条-1 视频地址: https://www.bilibili.com/video/av39709290/?p=10 博客地址: https ...
- Swift - 带结果列表的搜索条(UISearchDisplayController)的用法
(注:自iOS8起,苹果便废弃UISearchDisplayController的使用,改为使用UISearchController来实现类似功能,可参考我的另一篇文章“Swift - 使用UISea ...
- Swift - 进度条(UIProgressView)的用法
1,创建进度条 1 2 3 4 var progressView=UIProgressView(progressViewStyle:UIProgressViewStyle.Default) progr ...
随机推荐
- activity入门2
1.如何获取其他应用的包名和类名? 点击和查看logcat第一条信息2.第二步Intent intent = new Intent();intent.setClassName("com.an ...
- winform之2---messagebox用法
MessageBox.Show();可谓是winform开发中用的次数最多的东东啦.先贴一张msdn的图解 msdn好像没有更新哎,只提供了这几种方法,并且参数名称和最新的有差别,但实际上messag ...
- 【转】windows 7系统安装与配置Tomcat服务器环境
原文链接: windows 7系统安装与配置Tomcat服务器环境 工具/原料 jdk-8u51-windows-x64(我的系统是64位系统,32位的请选x86下载)下载地址:http://www. ...
- python-Day5-深入正则表达式--冒泡排序-时间复杂度 --常用模块学习:自定义模块--random模块:随机验证码--time & datetime模块
正则表达式 语法: mport re #导入模块名 p = re.compile("^[0-9]") #生成要匹配的正则对象 , ^代表从开头匹配,[0 ...
- javascript笔记整理(正则)
RegExp 对象表示正则表达式,它是对字符串执行模式匹配的强大工具 var re=/e/; var re=new RegExp('e'); 正则表达式的 String 对象的方法 1.search- ...
- android开发中防止刚进入activity时edittext获取焦点,自动弹出软键盘
刚进入activity的时候,如果布局组件有edittext的话,往往edittext会获取焦点,自动弹出软键盘,影响整个界面的视觉效果.解决方法如下: 可以在edittext的父布局结构中(例如 ...
- 已知一指针p,你可以确定该指针是否指向一个有效的对象吗?如果可以,如何确定?如果不可以,请说明原因。
这个问题我的思路是:首先用*p将其值输出来,如果编译器报错,证明p指向一个无效的对象,要么p=0要么p未进行初始化,此时可以用if(p == NULL)进行判断即可,不知道大家是否有好的思路噻...
- 基于visual Studio2013解决C语言竞赛题之0902文件查找
题目
- Python与开源GIS:在OGR中使用SQL语句进行查询
摘要: 属性选择与空间选择都可以看作是OGR内置的选择功能,这两种功能可以解决大部分实际中的问题.但是也有这种时候,就是进行查询时的条件比较复杂.针对这种情况,OGR也提供了更加灵活的解决方案:支持使 ...
- win7系统远程连接其它计算机,并且向远程机传输文件
首先,打开开始菜单,在程序自带的 “附件“ 中找到 "远程桌面连接"并打开,出现远程桌面对话框: 其次,在对话框左下角点击“选项”,选择“本地资源对话框”,在本地设备和资源下点击“ ...