IOS UISearchBar
2015-12-10 23:10
316 查看
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.
}
}
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.
}
}
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="9059" systemVersion="14F27" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" initialViewController="BYZ-38-t0r"> <dependencies> <deployment identifier="iOS"/> <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="9049"/> </dependencies> <scenes> <!--View Controller--> <scene sceneID="tne-QT-ifu"> <objects> <viewController id="BYZ-38-t0r" customClass="ViewController" customModule="UISearchBar" customModuleProvider="target" sceneMemberID="viewController"> <view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC"> <rect key="frame" x="0.0" y="0.0" width="320" height="568"/> <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/> <subviews> <tableView clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" rowHeight="44" sectionHeaderHeight="22" sectionFooterHeight="22" id="eUR-Ky-A8I"> <rect key="frame" x="6" y="32" width="320" height="440"/> <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/> <color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/> <searchBar key="tableHeaderView" contentMode="redraw" id="8pv-hH-OQ9"> <rect key="frame" x="0.0" y="0.0" 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> <point key="canvasLocation" x="130.66666666666666" y="220.09999999999999"/> </scene> </scenes> </document>
相关文章推荐
- UI基础-数据库
- IOS UISearchBar(一)
- UI-第三讲:视图控制器
- IOS UITableView (五)
- 对web的request的全部值进行了提取
- easyui
- UIScrollView, UIPageControl 的详细使用
- Devexpress VCL Build v2015 vol 15.2 开始测试
- 让UITableView的section header view不悬停的方法
- 【android_温故知新】第 6 组 UI 组件:ViewAnimator 及其子类
- bzoj:3392: [Usaco2005 Feb]Part Acquisition 交易
- Special Subsequence(离散化线段树+dp)
- UIImagePickerController  本地照片
- CALayer 初学(1)
- UE4 蓝图函数库
- UIButtonn-SB
- iOS系列UI篇——UIDatePicker和UIPickerView
- iOS之UI--关于modal
- 01-事件处理简介/UIView拖拽
- UE4 执行Console Command ----ExecuteConsoleCommand