NSPredicate和搜索栏的使用

这一节主要来讲一下 搜索栏联合NSPredicate谓词过滤来进行判断分析的情况 searchBar有委托方法 但不要求数据源 还有一个NSPredicate方法的谓词匹配法 SELF 表示对象 随后contain表示包含这个字符 随后c表示不区分大小写 当得到谓词匹配式以后 就来刷新数组 NSArray和NSMutableArray有不同的方法 随后刷新数组 刷新列表即可

//
//  ViewController.swift
//  表视图
//
//  Created by 金阳 on 16/1/22.
//  Copyright © 2016年 金阳. All rights reserved.
//

import UIKit

class ViewController: UIViewController,UISearchBarDelegate,UITableViewDataSource,UITableViewDelegate{
//查询的数组
    @IBOutlet weak var TableView: UITableView!
    var array:NSMutableArray = ["cao","kai","qiang","ni","hao","shuai"]
    //listArray为array的子集
    var listArray = NSMutableArray()
    @IBOutlet weak var searchBar: UISearchBar!
    override func viewDidLoad() {
        TableView.delegate = self
        TableView.dataSource = self
        //设置搜索栏的委托对象为当前的视图
        searchBar.delegate = self
        //设置搜索范围栏的隐藏 当开始搜索时 范围栏才显示
        searchBar.showsScopeBar = false
        //重新设置搜索框的大小
        searchBar.sizeToFit()
        //初次进入时查询所有的数据
        self.filterContentForSearchText("", scope: -1)
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
   
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    //为了查询方便自定义过滤的方法 searchText是要过滤的条件 scope是搜索的范围的索引
    func filterContentForSearchText(searchText:NSString,scope:Int) {
        if(searchText.length == 0) {
            //查询所有的数值
            listArray = NSMutableArray(array: array)
            return
        }
        var tempArray:NSArray!
        
        //中文的话
        if(scope == 0){
            let scopePredicate = NSPredicate(format: "SELF contains[c] %@", searchText)
            tempArray = array.filteredArrayUsingPredicate(scopePredicate)
            listArray = NSMutableArray(array: tempArray)
        }
        //是英文的字段
        else if(scope == 1) {
            let scopePredicate = NSPredicate(format:"SELF CONTAINS[c] %@", searchText)
            tempArray = array.filteredArrayUsingPredicate(scopePredicate)
            listArray = NSMutableArray(array: tempArray)
        }else{
            listArray = NSMutableArray(array: array)
        }
    }
//实现searchBar的委托方法 获得焦点 成为第一响应者
    func searchBarShouldBeginEditing(searchBar: UISearchBar) -> Bool {
        searchBar.showsScopeBar = true
        searchBar.sizeToFit()
        return true
        }
//单击键盘上的搜算按钮时
    func searchBarSearchButtonClicked(searchBar: UISearchBar) {
        searchBar.showsScopeBar = false
        //放弃成为第一响应者
        searchBar.resignFirstResponder()
        searchBar.sizeToFit()
    }
//单击搜索栏时取消按钮
    func searchBarCancelButtonClicked(searchBar: UISearchBar) {
        //查询所有的情况
        filterContentForSearchText("", scope: -1)
        searchBar.showsScopeBar = false
        searchBar.resignFirstResponder()
        searchBar.sizeToFit()
    }
//当文本内容发生改变时进行调用
    func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
        filterContentForSearchText(searchText, scope: searchBar.selectedScopeButtonIndex)
        //刷新tableView
        self.TableView.reloadData()
    }
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let id = "id"
        let cell = UITableViewCell(style: .Default, reuseIdentifier: id)
        let row = indexPath.row
        cell.textLabel?.text = listArray[row] as? String
        return cell
    }
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return listArray.count
    }
    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }
}
终于搞出来了 这货

你可能感兴趣的:(NSPredicate和搜索栏的使用)