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

Swift - 使用UISearchController实现带搜索栏的表格

发布时间:2020-12-14 07:21:07 所属栏目:百科 来源:网络整理
导读:我原来写过一篇文章“ Swift - 带结果列表的搜索条(UISearchDisplayController)的用法 ”,当时是使用 UISearchDisplayController 来实现带有搜索功能的列表,由于 本身就整合了搜索条和表格,所有用起来很方便。 到了iOS8,苹果废除 UISearchDisplayContr
我原来写过一篇文章“ Swift - 带结果列表的搜索条(UISearchDisplayController)的用法 ”,当时是使用 UISearchDisplayController 来实现带有搜索功能的列表,由于 本身就整合了搜索条和表格,所有用起来很方便。

到了iOS8,苹果废除 UISearchDisplayController,建议我们使用 UISearchController配合 UITableView来实现。我们可以把搜索条放在表格头部,或者放在页面顶部,还是很灵活的。下面通过代码演示如何使用 UISearchController实现具有搜索功能的表格。

效果图如下:

代码如下:
(注:这里对ViewController做了类扩展ViewControllerExtensions.swift,把UITableView和UISearchController的代理方法都写在扩展类里,使代码更加简洁)
--- ViewController.swift ---
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
import UIKit
class ViewController : UIViewController {
//展示列表
var tableView: UITableView !
//搜索控制器
countrySearchController = UISearchController ()
//原始数据集
let schoolArray = [ "清华大学" , "北京大学" "中国人民大学" "北京交通大学" "北京工业大学" "北京航空航天大学" "北京理工大学" "北京科技大学" "中国政法大学" "中央财经大学" "华北电力大学" "北京体育大学" "上海外国语大学" "复旦大学" "华东师范大学" "上海大学" "河北工业大学" ]
//搜索过滤后的结果集
searchArray:[ String ] = [ ](){
didSet { self .tableView.reloadData()}
}
override func viewDidLoad() {
super .viewDidLoad()
//创建表视图
.tableView = (frame: UIScreen .mainScreen().applicationFrame,
style: UITableViewStyle . Plain )
.tableView!.delegate = self
.tableView!.dataSource = self
//创建一个重用的单元格
.tableView!.registerClass( UITableViewCell . forCellReuseIdentifier: "MyCell" )
.view.addSubview( .tableView!)
//配置搜索控制器
.countrySearchController = ({
controller = (searchResultsController: nil )
controller.searchResultsUpdater = self
controller.hidesNavigationBarDuringPresentation = false
controller.dimsBackgroundDuringPresentation = false
controller.searchBar.searchBarStyle = . Minimal
controller.searchBar.sizeToFit()
.tableView.tableHeaderView = controller.searchBar
return controller
})()
}
viewDidAppear(animated: Bool ) {
.viewDidAppear( true )
.tableView.reloadData()
}
didReceiveMemoryWarning() {
.didReceiveMemoryWarning()
}
}

--- ViewControllerExtensions.swift ---
56
57
58
59
60
Foundation
UIKit
extension UITableViewDataSource
{
tableView(tableView: Int ) -> Int
{
if ( .countrySearchController.active)
{
return .searchArray.count
} else
{
.schoolArray.count
}
}
NSIndexPath )
-> UITableViewCell
{
//为了提供表格显示性能,已创建完成的单元需重复使用
identify: = "MyCell"
//同一形式的单元格重复使用,在声明时已注册
cell = tableView.dequeueReusableCellWithIdentifier(identify,
forIndexPath: indexPath)
.countrySearchController.active)
{
cell.textLabel?.text = .searchArray[indexPath.row]
cell
}
else
{
.schoolArray[indexPath.row]
cell
}
}
}
UITableViewDelegate
{
)
{
tableView.deselectRowAtIndexPath(indexPath,animated: )
}
UISearchResultsUpdating
{
updateSearchResultsForSearchController(searchController: )
{
.searchArray.removeAll(keepCapacity: false )
searchPredicate = NSPredicate (format: "SELF CONTAINS[c] %@" searchController.searchBar.text!)
array = ( .schoolArray as NSArray )
.filteredArrayUsingPredicate(searchPredicate)
.searchArray = array as ! [ ]
}
原文出自: www.hangge.com 转载请保留原文链接: http://www.hangge.com/blog/cache/detail_797.html

(编辑:李大同)

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