UISearchController

search.png

一、简单添加

@1
var sc : UISearchController!
@2代理处遵守
UISearchResultsUpdating
@3
sc = UISearchController(searchResultsController: nil)
sc.searchResultsUpdater = self
tableView.tableHeaderView = sc.searchBar
可能存在的bug

此时,页面上就会出现一个搜索栏了,如果你点击它,搜索栏消失了,那么就检查一下,是否将导航栏的半透明效果取消了,如果是,就会出现这个bug。

如何解决bug

使导航控制器的扩展边缘属性,包含不透明
Extend Edges --> Under Opaque Bars

self.navigationController?.extendedLayoutIncludesOpaqueBars = true

二、筛选器

搜索控制器没有现成的筛选控功能,需要手动添加筛选规则

@1对于本练习demo而言,需要加上:按地区名称匹配
//定义一个空数组以保存搜索结果
    var searchResults : [AreaMO] = []
@2添加一个筛选方法
//添加一个筛选器方法
    func searchFilter(text: String) {
        //Swift数组自带的filter方法/功能:返回一个符合条件的新数组
        searchResults = areas.filter({ (area) -> Bool in
            //Contains检测字符串中包含另一个字符串
            return area.name!.localizedCaseInsensitiveContains(text)
        })
    }

三、更新搜索结果

@1、首先遵从UISearchResultsUpdating协议
@2、此协议定义了一个方法:updateSearchResults
@3、当用户点击搜索条,或者更改搜索文字,这个方法被调用
@4、通过改方法,让搜索控制器显示搜索结果
// MARK: - UISearchResultsUpdating
    func updateSearchResults(for searchController: UISearchController) {
        if let text = searchController.searchBar.text {
            searchFilter(text: text)
            tableView.reloadData()
        }
    }

四、搜索与列表互动

@1、当搜索栏在使用时,isActive属性为true
@2、列表的numberOfRowsInSection方法:
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

//        return areas.count
        return sc.isActive ? searchResults.count : areas.count
    }
@3、列表的cellForRowAt indexPath方法:

原代码:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CustomTableViewCell

        cell.nameLab.text = areas[indexPath.row].name
        cell.thumbImage.image = UIImage(data: areas[indexPath.row].thumbImage!)
        cell.proviceLab.text = areas[indexPath.row].province
        cell.partLab.text = areas[indexPath.row].part
        
        cell.thumbImage.layer.cornerRadius = cell.thumbImage.frame.size.width/2
        cell.thumbImage.layer.masksToBounds = true
        
        cell.accessoryType = areas[indexPath.row].visited ? .checkmark : .none
        
        return cell
    }

现代码:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CustomTableViewCell

        let area = sc.isActive ? searchResults[indexPath.row] : areas[indexPath.row]
        
        cell.nameLab.text = area.name
        cell.thumbImage.image = UIImage(data: area.thumbImage!)
        cell.proviceLab.text = area.province
        cell.partLab.text = area.part
        
        cell.thumbImage.layer.cornerRadius = cell.thumbImage.frame.size.width/2
        cell.thumbImage.layer.masksToBounds = true
        
        cell.accessoryType = area.visited ? .checkmark : .none
        
        return cell
    }
4、搜索显示时,不可编辑:
    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        return !sc.isActive
    }
5、列表的点击方法
let action3 = UIAlertAction(title: "进入详情页", style: .destructive) { (_) in
            
            let detailTVC = self.storyboard?.instantiateViewController(withIdentifier: "DetailTableViewController") as! DetailTableViewController
            detailTVC.area = self.sc.isActive ? self.searchResults[indexPath.row] : self.areas[indexPath.row]
            self.navigationController?.pushViewController(detailTVC, animated: true)
            
        }

五、搜索栏存在的bug

假如有一组数据aaaaa,当在搜索栏中打入aaa aa[含空格]是时,就会查找不到,此时需要做去空格操作


你可能感兴趣的:(UISearchController)