8.0后可以用UISearchController快速实现搜索控制器功能,如下效果:
这个小功能好似app的标配一样,挺多应用有这样的功能,无事也记录一版(swift)
UISearchController需要一个自定义的resultController来初始化。
1.首先新建一个ModuleTwoVC类来演示如何集成,上代码。
ModuleTwoVC.swift,需要实现协议:UISearchResultsUpdating,UISearchBarDelegate
import UIKit
class ModuleTwoVC: McBaseViewController,UITableViewDataSource,UITableViewDelegate,UISearchResultsUpdating,UISearchBarDelegate{
static let cellIdentifer = "myCell"
var searchController: UISearchController?
var tableView: UITableView?
var itemArray : Array = [String]()
var tempsArray : Array = [String]()
func initData()
{
for i in 0 ..< 30 {
let str = "夜如何其\(i)……………………"
itemArray.append(str)
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.setCustomTitle("发现")
self.definesPresentationContext = true
self.initData()
self.initSubView()
}
func initSubView()
{
self.tableView = UITableView(frame: CGRectMake(0, 0, McConstant.screenWidth, McConstant.screenHeight), style: UITableViewStyle.Plain)
self.tableView?.backgroundColor = UIColor.clearColor()
self.tableView?.dataSource = self
self.tableView?.delegate = self
let aaaView = UIView()
aaaView.backgroundColor = UIColor.whiteColor()
self.tableView?.tableFooterView = aaaView
self.view.addSubview(self.tableView!)
let resultVC = UINavigationController(rootViewController: McSearchResultVC())
self.searchController = UISearchController(searchResultsController: resultVC)
self.searchController?.searchResultsUpdater = self
self.searchController?.searchBar.frame = CGRect(x: 0, y: 0, width: McConstant.screenWidth, height: 44)
self.searchController?.searchBar.delegate = self
self.tableView?.tableHeaderView = self.searchController?.searchBar
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return itemArray.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier(ModuleOneVC.cellIdentifer)
if cell == nil {
cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: ModuleOneVC.cellIdentifer)
}
cell?.textLabel?.text = itemArray[indexPath.row]
cell?.textLabel?.textColor = UIColor.blackColor()
cell?.backgroundColor = UIColor.clearColor()
return cell!
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 50
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
//do somthing...
}
//mark - UISearchResultsUpdating
func updateSearchResultsForSearchController(searchController: UISearchController) {
//
let nav_VC = self.searchController?.searchResultsController as! UINavigationController
let resultVC = nav_VC.topViewController as! McSearchResultVC
self.searchContentForText((self.searchController?.searchBar.text)!)
resultVC.itemArray = self.tempsArray
resultVC.tableView?.reloadData()
}
func searchContentForText(searchText: String)
{
self.tempsArray.removeAll()
for str in self.itemArray {
//
if str.componentsSeparatedByString(searchText).count > 1 {
self.tempsArray.append(str)
}
}
}
}
2.resultController实现。。。新建一个自定的resultController类
McSearchResultVC.swift
import UIKit
class McSearchResultVC: McBaseViewController,UITableViewDataSource,UITableViewDelegate {
static let cellIdentifer = "myCell"
var tableView: UITableView?
var itemArray : Array = [String]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
self.intiSubView()
}
func intiSubView()
{
self.tableView = UITableView(frame: CGRectMake(0, 0, McConstant.screenWidth, McConstant.screenHeight), style: UITableViewStyle.Plain)
self.tableView?.backgroundColor = UIColor.clearColor()
self.tableView?.dataSource = self
self.tableView?.delegate = self
let aaaView = UIView()
aaaView.backgroundColor = UIColor.whiteColor()
self.tableView?.tableFooterView = aaaView
self.view.addSubview(self.tableView!)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return itemArray.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier(ModuleOneVC.cellIdentifer)
if cell == nil {
cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: ModuleOneVC.cellIdentifer)
}
cell?.textLabel?.text = itemArray[indexPath.row]
cell?.textLabel?.textColor = UIColor.blackColor()
cell?.backgroundColor = UIColor.clearColor()
return cell!
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 50
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: true)
//do somthing...
}
}
上效果图: