Swift系列之——UISearchBar的简单使用
来源:互联网 发布:2016淘宝热卖行业 编辑:程序博客网 时间:2024/06/06 15:49
本篇博客的语法适用于Swift3.0以上。
UISearchBar是一个苹果自带的搜索条,由一个文本框和几个按钮组成,当用户在文本框内输入部分内容之后,程序即可按照指定的规则执行搜索。
下面是UISearchBar的长相:
//// ViewController.swift// UISearchBarTest//// Created by Mac on 2017/8/4.// Copyright © 2017年 Jing. All rights reserved.//import UIKitclass ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate { var tableArray: NSArray = ["UI", "UIView", "View", "ViewController", "Controller", "UIViewController", "Search", "UISearchBar", "Swift"] var searchArray: NSArray = [] var isSearch = false//默认在非搜索状态下 override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. self.tableView.tableHeaderView = self.searchBar; self.view.addSubview(tableView); } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } //使用懒加载方式来创建UITableView lazy var tableView: UITableView = { let tempTableView = UITableView (frame: self.view.bounds, style: UITableViewStyle.plain) tempTableView.delegate = self tempTableView.dataSource = self tempTableView.tableFooterView = UIView.init() return tempTableView }() //使用懒加载方式来创建UISearchBar lazy var searchBar: UISearchBar = { let tempSearchBar = UISearchBar(frame:CGRect(x: 0, y: 64, width: self.view.bounds.size.width, height: 40))// tempSearchBar.prompt = "查找图书"; tempSearchBar.placeholder = "请输入搜索关键字"; tempSearchBar.showsCancelButton = true; tempSearchBar.delegate = self return tempSearchBar }() //根据输入的关键字来过滤搜索结果 func filterBySubstring(filterStr: NSString!) { isSearch = true let predicate = NSPredicate(format: "SELF CONTAINS[c] %@", filterStr) searchArray = tableArray.filtered(using: predicate) as NSArray tableView.reloadData() } //MARK: UITableViewDelegate func numberOfSections(in tableView: UITableView) -> Int { return 1 } func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { if isSearch { return searchArray.count } else{ return tableArray.count } } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let identifier = "cellId" var cell = tableView.dequeueReusableCell(withIdentifier: identifier) if cell == nil { cell = UITableViewCell.init(style: UITableViewCellStyle.default, reuseIdentifier: identifier) } let row = indexPath.row if isSearch { cell?.textLabel?.text = searchArray[row] as? String } else{ cell?.textLabel?.text = tableArray[row] as? String } return cell! } //MARK: UISearchBarDelegate func searchBarCancelButtonClicked(_ searchBar: UISearchBar) { isSearch = false searchBar.resignFirstResponder() tableView.reloadData() } func searchBarSearchButtonClicked(_ searchBar: UISearchBar) { filterBySubstring(filterStr: searchBar.text! as NSString) } func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) { filterBySubstring(filterStr: searchText as NSString) }}
阅读全文
1 0
- Swift系列之——UISearchBar的简单使用
- swift中UISearchBar的使用
- swift中UISearchBar的使用
- 【swift系列之UIButton】简单使用
- iOS之UISearchBar的使用
- IOS系列——UIsearchBar
- swift 搜索框(UISearchBar)实现简单的搜索功能
- Swift之NSNumberFormatter的简单使用
- Swift基础之Literal的简单使用
- UISearchBar的简单实用
- swift 搜索框(UISearchBar)实现简单的搜索功能(swift3)
- UISearchBar的使用
- UISearchBar 的使用
- UISearchBar的使用
- UISearchBar 的使用
- UISearchBar的使用
- UISearchBar的使用
- UISearchBar的使用
- 数据冒险之二叉树(链表实现)
- 微信小程序入门示例
- 代码优化:SessionFactory过期项
- 在K8s中创建StatefulSet
- ios 消息转发机制
- Swift系列之——UISearchBar的简单使用
- Hadoop-2.7.2集群的搭建——集群学习日记
- 【面试题】计算机网络相关
- composer部署Yii2框架(linux)遇到问题及解决思路总结
- 1181: 谁的年龄最小(结构体专题)
- 字符串转换
- 华为OJ——统计大写字母个数
- ld: framework not found Pods
- [RK3288][Android6.0] 调试笔记 --- 低电压引起的关机问题