加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Swift - 搜索条(UISearchBar)的用法

发布时间:2020-12-14 02:45:59 所属栏目:百科 来源:网络整理
导读:Swift - 搜索条(UISearchBar)的用法 http://www.hangge.com/blog/cache/detail_562.html 1,下面是一个搜索条的使用样例,功能如下: (1)在Main.storyboard界面里拖入一个Search Bar和一个Table View,Search Bar放到Table View的页眉位置 (2)初始化或

Swift - 搜索条(UISearchBar)的用法


http://www.hangge.com/blog/cache/detail_562.html

1,下面是一个搜索条的使用样例,功能如下:
(1)在Main.storyboard界面里拖入一个Search Bar和一个Table View,Search Bar放到Table View的页眉位置
(2)初始化或者搜索条为空时,表格显示所有数据
(3)搜索条不为空时,表格实时过滤显示匹配的项目

2,效果图

3,代码如下
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 !
tableView : UITableView !
// 所有组件
ctrls:[ String ] = [ "Label" "Button1" "Button2" "Switch" ]
// 搜索匹配的结果,Table View使用这个数组作为datasource
ctrlsel:[ String ] = []
override func viewDidLoad() {
super .viewDidLoad()
// 起始加载全部内容
self .ctrlsel = .ctrls
// 注册TableViewCell
.tableView.registerClass( UITableViewCell . "SwiftCell" )
}
// 返回表格行数(也就是返回控件数)
tableView(tableView: UITableView Int ) -> Int {
return .ctrlsel.count
}
// 创建各单元显示内容(创建参数indexPath指定的单元)
tableView(tableView: NSIndexPath )
-> UITableViewCell
{
// 为了提供表格显示性能,已创建完成的单元需重复使用
let identify: = "SwiftCell"
// 同一形式的单元格重复使用,在声明时已注册
cell = tableView.dequeueReusableCellWithIdentifier(identify,forIndexPath: indexPath)
as UITableViewCell
cell.accessoryType = UITableViewCellAccessoryType . DisclosureIndicator
cell.textLabel?.text = .ctrlsel[indexPath.row]
return cell
}
// 搜索代理UISearchBarDelegate方法,每次改变搜索内容时都会调用
searchBar(searchBar: !,textDidChange searchText: !) {
// 没有搜索内容时显示全部组件
if searchText == "" {
.ctrls
}
else { // 匹配用户输入内容的前缀
.ctrlsel = []
for ctrl in .ctrls {
ctrl.lowercaseString.hasPrefix(searchText) {
.ctrlsel.append(ctrl)
}
}
}
// 刷新Table View显示
.tableView.reloadData()
}
// 搜索代理UISearchBarDelegate方法,点击虚拟键盘上的Search按钮时触发
//func searchBarSearchButtonClicked(searchBar: UISearchBar!) {
//searchBar.resignFirstResponder()
//}
didReceiveMemoryWarning() {
.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}

--- Main.storyboard ---
47
<? xml version = "1.0" encoding "UTF-8" standalone "no" ?>
< document type "com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" "3.0" toolsVersion "6254" systemVersion "14B25" targetRuntime "iOS.CocoaTouch" propertyAccessControl "none" useAutolayout "YES" useTraitCollections initialViewController "BYZ-38-t0r" >
dependencies >
plugIn identifier "com.apple.InterfaceBuilder.IBCocoaTouchPlugin" "6247" />
</ >
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 "top" "y3c-jy-aDJ" />
"bottom" "wfy-db-euE" />
>
view key "view" contentMode "scaleToFill" "8bC-Xf-vdC" >
rect "frame" x "0.0" y width "600" height "600" />
autoresizingMask "autoresizingMask" widthSizable heightSizable "YES" />
subviews >
tableView clipsSubviews fixedFrame alwaysBounceVertical dataMode "prototypes" style "plain" separatorStyle "default" rowHeight "44" sectionHeaderHeight "22" sectionFooterHeight translatesAutoresizingMaskIntoConstraints "NO" "eUR-Ky-A8I" >
"6" "32" "320" "440" />
color "backgroundColor" white "1" alpha colorSpace "calibratedWhite" />
searchBar "tableHeaderView" "redraw" "8pv-hH-OQ9" >
"80" "218" "44" />
flexibleMaxX flexibleMaxY />
textInputTraits "textInputTraits" />
connections >
outlet property "delegate" destination "0l9-UU-iHJ" />
>
searchBar >
>
"dataSource" "o1t-B2-xHp" />
"p1t-kn-J9Q" />
>
tableView >
>
"custom" customColorSpace />
view >
>
"searchBar" "8pv-hH-OQ9" "B0M-ya-PE5" />
"tableView" "eUR-Ky-A8I" "YCI-P6-0gY" />
>
viewController >
placeholder placeholderIdentifier "IBFirstResponder" "dkx-z0-nzr" "firstResponder" />
>
scene >
>
document >
上一篇: Swift - 将表格UITableView滚动条移动到底部

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读