swift 搜索框(UISearchBar)实现简单的搜索功能
来源:互联网 发布:数据库做学生管理系统 编辑:程序博客网 时间:2024/06/05 07:15
1.在storyboard里面拖入一个 Search Bar 和 一个 tableView。
2.创建ViewController,实现代理:UISearchBarDelegate,UITableViewDataSource,UITableViewDelegate.
代码实现如下:
@IBOutlet weak var searchBar: UISearchBar!@IBOutlet weak var tableView: UITableView!let array = ["beijing", "shanghai","guangzhou","shenzhen" ,"changsha","wuhan","tianjing","hangzhou"]var result = [String]()override func viewDidLoad() { super.viewDidLoad() // 搜索内容为空时,显示全部内容 self.result = self.array self.searchBar.delegate = self self.tableView.delegate = self self.tableView.dataSource = self self.searchBar.placeholder = "搜索" // 注册tableviewCell self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")}
// tableview 代理方法
extension ViewController:UITableViewDataSource,UITableViewDelegate {
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return self.result.count}func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let identify: String = "cell" let cell = tableView.dequeueReusableCellWithIdentifier(identify, forIndexPath: indexPath) as UITableViewCell cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator cell.textLabel?.text = self.result[indexPath.row] return cell}
}
// UISearchBar 代理方法
// 每次改变搜索内容时候调用此方法
extension ViewController: UISearchBarDelegate {
func searchBar(searchBar: UISearchBar, textDidChange searchText: String) { print("[ViewController searchBar] searchText: \(searchText)") // 没有搜索内容时显示全部内容 if searchText == "" { self.result = self.array } else { // 匹配用户输入的前缀,不区分大小写 self.result = [] for arr in self.array { if arr.lowercaseString.hasPrefix(searchText.lowercaseString) { self.result.append(arr) } } } // 刷新tableView 数据显示 self.tableView.reloadData()}// 搜索触发事件,点击虚拟键盘上的search按钮时触发此方法func searchBarSearchButtonClicked(searchBar: UISearchBar) { searchBar.resignFirstResponder()}// 书签按钮触发事件func searchBarBookmarkButtonClicked(searchBar: UISearchBar) { print("搜索历史")}// 取消按钮触发事件func searchBarCancelButtonClicked(searchBar: UISearchBar) { // 搜索内容置空 searchBar.text = "" self.result = self.array self.tableView.reloadData()}
}
实现效果如下:
0 0
- swift 搜索框(UISearchBar)实现简单的搜索功能
- swift 搜索框(UISearchBar)实现简单的搜索功能(swift3)
- UISearchBar, NSPredicate 实现简单的搜索功能
- Swift - 搜索条(UISearchBar)的用法
- Swift - 搜索条(UISearchBar)的用法
- ios搜索框功能的实现/UISearchBar/UISearchController的使用
- 实现UISearchBar空字符串搜索 swift
- 纯代码实现UISearchBar搜索功能
- UISearchBar套接TableView实现搜索功能
- iOS UISearchBar 搜索功能
- UItableView和UISearchBar实现简单表格搜索
- 使用搜索条UISearchBar 谓词实现搜索的判断
- thymeleaf实现简单的条件搜索并分页搜索功能
- UISearchBar 搜索框
- UISearchBar搜索框用法
- iOS搜索框UISearchBar
- UI UISearchBar搜索框
- UISearchBar 搜索框
- Qt加载本地html文件 并且调用JS的函数在Qt中使用
- Mysql 求时间 between 昨天 and 上个月的今天 等时间函数
- Spring 3.x jar 包详解 与 依赖关系
- delphi的Public,Private,Protected,Published作用域
- linux工作调度 at、cron
- swift 搜索框(UISearchBar)实现简单的搜索功能
- g++生成so动态库文件
- 排序算法
- POJ 3693 后缀数组+RMQ
- ListView 几个重要属性
- 下拉刷新和分页加载
- javascript深入理解js闭包
- Android中dp和px之间进行转换(工具类)
- Unity3d基于第三人称控制简单优化