使用swift搜索类数组对象

时间:2016-12-21 19:19:11

标签: ios swift

我有一个问题,我想添加UISearchController但是搜索谓词不执行搜索,因为我的代码结构就像我有一个Class of Class这个项目可以先在数据库中获取并存储在单例中,之后我将通过索引获取对象和类数组,这是我的代码

MasterViewController:

import UIKit

class MasterTableViewController: UITableViewController, UISplitViewControllerDelegate, UISearchResultsUpdating {

    var array: NSMutableArray! = NSMutableArray()

    var itemArray = Array<itemClass>()

    var filtereditemArray = [String]()
    var searchController = UISearchController(searchResultsController: nil)


    override func viewDidLoad() {
        super.viewDidLoad()


        self.tableView.reloadData()

        self.splitViewController!.delegate = self;
        self.splitViewController!.preferredDisplayMode = UISplitViewControllerDisplayMode.allVisible

        //Seach Result Controller
        self.searchController.searchResultsUpdater = self
        self.searchController.dimsBackgroundDuringPresentation = false
        self.searchController.definesPresentationContext = true
        self.searchController.searchBar.sizeToFit()
        self.tableView.tableHeaderView = self.searchController.searchBar


    }


    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat{
        return 60
    }


    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        if searchController.isActive{
            return filtereditemArray.count
        }
        else{
            return itemArray.count
        }
    }

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


        cell.textLabel?.adjustsFontSizeToFitWidth = true
        cell.textLabel?.lineBreakMode = NSLineBreakMode.byWordWrapping
        cell.textLabel?.numberOfLines = 3


        cell.textLabel?.textColor = UIColor.black
        cell.textLabel?.font = UIFont(name: "ChalkboardSE-Light", size: 14.0)


        if searchController.isActive {
            //let fra = filtereditemArray[indexPath.row]
            //cell.textLabel?.text = fra.title
        }
        else{
            let fd = itemArray[indexPath.row]
            cell.textLabel?.text = fd.title.uppercased()
        }

        return cell
    }

    func updateSearchResults(for searchController: UISearchController) {

        self.filtereditemArray.removeAll(keepingCapacity: false)
        let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text!)

        let array = (itemClass as NSArray).filtered(using: searchPredicate)

        self.filtereditemArray = array as! [String]


        self.tableView.reloadData()


    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

        print("DidSelect index Path: \(indexPath.row)")
        //print("Filtered item Index: \(filtereditemArray[indexPath.row]))")

        self.performSegue(withIdentifier: "showDetail", sender: self)

    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        if segue.identifier == "showDetail" {

            let index = self.tableView.indexPathForSelectedRow! as IndexPath

            let nav = segue.destination as! UINavigationController

            let vc = nav.viewControllers[0] as! DetailViewController

            if searchController.isActive {

        // pending

            }
            else{
                vc.selecteditemArray = [itemArray[index.row]]

            }
            self.tableView.deselectRow(at: index, animated: true)

        }

    }    

}

itemClass时:

import Foundation

class itemClass: NSObject{

    var id: String = String()
    var title: String = String()
    var category: String = String()

}

0 个答案:

没有答案