我想在我的应用程序中使用搜索栏。我正在尝试使用它,但即将出现异常。我有一个名为成员[[String:Anyobject]]的字典数组,从中我取出了名称,并存储到字符串类型的数组数据中,它不起作用。

这是我的代码:

import UIKit

class hcbaViewController: UIViewController,UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate {

@IBOutlet var searchbar: UISearchBar!

@IBOutlet var tableview: UITableView!

var member = [[String:AnyObject]]()

var members = [String:AnyObject]()

var searchActive = true
var filtered:[String] = []
var data: [String] = []


override func viewDidLoad() {
    super.viewDidLoad()
    print(data)
    print("________-----------________----------")
 print(member)


    // Do any additional setup after loading the view.
}

func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
    searchActive = true
}

func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
    searchActive = false
}
func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
    searchActive = false
}
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
    searchActive = false
}


func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {

    filtered = data.filter({ (text) -> Bool in
        let tmp:NSString = text as NSString
        let range = tmp.range(of: searchText, options: NSString.CompareOptions.caseInsensitive)
        return range.location != NSNotFound
    })

    if (filtered.count == 0){
        searchActive = false
    }
    else{
        searchActive = true
    }
    self.tableview.reloadData()
}

 func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {

    return "MemberDirectory"

}

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

    return member.count

   if(searchActive){
       return filtered.count
    }
 else{
        return data.count
    }

   // return member.count

}


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell: UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell",for: indexPath)

     var display = member[indexPath.row]

    cell.textLabel?.text = display["Name"] as! String?
    cell.detailTextLabel?.text = display["email"] as? String


    let n = display["Name"] as! String
     data.append(n)

    return cell
}


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

        let vc = segue.destination as! hcbadetailViewController
        vc.kk = members

}


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

       members = member[indexPath.row]
       self.performSegue(withIdentifier: "bye", sender: nil)
}

最佳答案

你可以试试这个...

class SearchNew: UIViewController, UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate, GADInterstitialDelegate{

    var SearchBarValue:String!
    var searchActive : Bool = false
    var data : NSMutableArray!
    var filtered:NSMutableArray!

    @IBOutlet var searchBar: UISearchBar!
    @IBOutlet var tableView: UITableView!


    override func viewDidLoad() {

        super.viewDidLoad()


        self.searchBar.showsCancelButton = false
        tableView.tableFooterView = UIView(frame: CGRectZero)
        /* Setup delegates */
        tableView.delegate = self
        tableView.dataSource = self
        searchBar.delegate = self

        self.searchBar.delegate = self
        data = []
        filtered = []


        self.getData()

    }  //-----viewDidLoad closed------


    func getData()
    {

           //insert member data within data array
            data.addObject(member)
    }


    func searchBarTextDidBeginEditing(_ searchBar: UISearchBar) {
        searchActive = true
    }

    func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
        searchActive = false
    }

    func searchBarCancelButtonClicked(searchBar: UISearchBar) {
        searchActive = false;

        searchBar.text = nil
        searchBar.resignFirstResponder()
        tableView.resignFirstResponder()
        self.searchBar.showsCancelButton = false
        tableView.reloadData()
    }

    func searchBarSearchButtonClicked(searchBar: UISearchBar) {
        searchActive = false
    }

    func searchBarShouldEndEditing(searchBar: UISearchBar) -> Bool {
                return true
    }


    func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {

            self.searchActive = true;
            self.searchBar.showsCancelButton = true


            filtered.removeAllObjects()

            dispatch_to_background_queue
                {
                    for  xdata in self.data
                    {
                        let nameRange: NSRange = xdata.rangeOfString(searchText, options: [NSStringCompareOptions.CaseInsensitiveSearch ,NSStringCompareOptions.AnchoredSearch ])

                        if nameRange.location != NSNotFound{

                            self.filtered.addObject(xdata)
                        }

                    }//end of for


                    self.dispatch_to_main_queue {
                        /* some code to be executed on the main queue */

                        self.tableView.reloadData()

            } //end of dispatch

        }


    }

    func dispatch_to_main_queue(block: dispatch_block_t?) {
        dispatch_async(dispatch_get_main_queue(), block!)
    }

    func dispatch_to_background_queue(block: dispatch_block_t?) {
        let q = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
        dispatch_async(q, block!)
    }



    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1
    }

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if(searchActive) {

            return filtered.count

        }else{
            return data.count
            }
    }


    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if (segue.identifier == "showDetailView") {
            if let destination=segue.destinationViewController as? DetailViewController{
                let path=tableView.indexPathForSelectedRow
                let cell=tableView.cellForRowAtIndexPath(path!)
                destination.passedValue=(cell?.textLabel?.text)
            }
        }
    }

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        searchBar.resignFirstResponder()
        searchBar.endEditing(true)
        self.view.endEditing(true)
        self.searchBar.showsCancelButton = false
        self.searchBar.text=""

    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("Cell")! as UITableViewCell;

        if(searchActive){
            cell.textLabel?.text = filtered[indexPath.row] as! NSString as String
        } else {
            cell.textLabel?.text = data[indexPath.row]as! NSString as String

        }

        return cell;
    }
}

希望对您有帮助。

关于ios - iOS Swift中的搜索栏,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43767766/

10-13 03:54