我一直在为搜索功能使用ios应用程序。目前,我设法使其正常运行,但是以这种方式,只有在用户完成文字输入并单击搜索按钮后,搜索才会开始。即使键入第一个字母,如何通过查询开始来使其快速响应?我目前正在使用带有UIView控制器而不是UITableViewController的搜索栏。

这是我的代码:

//
//  CoffeeListViewController.swift
//  CoffeeApp
//
//  Created by izzuddin on 14/03/2016.
//  Copyright © 2016 izzuddin. All rights reserved.
//

import UIKit
import CloudKit
import FBSDKCoreKit
import FBSDKLoginKit


class CoffeeListViewController: UIViewController {

    ////////////////////////////OUTLET

    @IBOutlet weak var tableview: UITableView!
    var coffees = [Coffee]()
    var filteredNames = [Coffee]()

    @IBOutlet weak var searchBar: UISearchBar!




    ////////////////////////////SPINNER

    var loadingView = UIView()
    var container = UIView()
    var activityIndicator = UIActivityIndicatorView()


    func showLoading() {

        let win:UIWindow = UIApplication.sharedApplication().delegate!.window!!
        self.loadingView = UIView(frame: win.frame)
        self.loadingView.tag = 1
        self.loadingView.backgroundColor = UIColor(red: 255/255, green: 255/255, blue: 255/255, alpha: 0)

        win.addSubview(self.loadingView)

        container = UIView(frame: CGRect(x: 0, y: 0, width: win.frame.width/3, height: win.frame.width/3))
        container.backgroundColor = UIColor(red: 0/255, green: 0/255, blue: 0/255, alpha: 0.6)
        container.layer.cornerRadius = 10.0
        container.layer.borderColor = UIColor.grayColor().CGColor
        container.layer.borderWidth = 0.5
        container.clipsToBounds = true
        container.center = self.loadingView.center


        activityIndicator.frame = CGRectMake(0, 0, win.frame.width/5, win.frame.width/5)
        activityIndicator.activityIndicatorViewStyle = .WhiteLarge
        activityIndicator.center = self.loadingView.center


        self.loadingView.addSubview(container)
        self.loadingView.addSubview(activityIndicator)

        activityIndicator.startAnimating()

    }

    func hideLoading(){
        UIView.animateWithDuration(0.0, delay: 1.0, options: .CurveEaseOut, animations: {
            self.container.alpha = 0.0
            self.loadingView.alpha = 0.0
            self.activityIndicator.stopAnimating()
            }, completion: { finished in
                self.activityIndicator.removeFromSuperview()
                self.container.removeFromSuperview()
                self.loadingView.removeFromSuperview()
                let win:UIWindow = UIApplication.sharedApplication().delegate!.window!!
                let removeView  = win.viewWithTag(1)
                removeView?.removeFromSuperview()
        })
    }





    ////////////////////////////LOAD DATA
    func call_data(){
        self.showLoading()

        let container = CKContainer.defaultContainer()
        let publicData = container.publicCloudDatabase

        let query = CKQuery(recordType: "Coffee", predicate: NSPredicate(format: "TRUEPREDICATE", argumentArray: nil))
        publicData.performQuery(query, inZoneWithID: nil) { results, error in
            if error == nil { // There is no error
                for coffee in results! {
                    let newCoffee = Coffee()
                    newCoffee.name = coffee["Name"] as! String
                    newCoffee.cafe = coffee["Cafe"] as! String
                    newCoffee.rating = coffee["Rating"] as! Double
                    newCoffee.place = coffee["Place"] as? CLLocation

                    self.coffees.append(newCoffee)


                    dispatch_async(dispatch_get_main_queue(), { () -> Void in
                        self.tableview.reloadData()
                        self.hideLoading()

                    })
                }
            }
            else {
                print(error)
            }
        }
    }




    //////////////////////////////////////////////////VIEW DID LOAD
    override func viewDidLoad() {
        super.viewDidLoad()
        call_data()

    }





    //////////////////////////////////////////////////PREPARE SEGUE

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {


        if segue.identifier == "AddCoffeeVC" {
            let destVC: AddCoffeeViewController = segue.destinationViewController as! AddCoffeeViewController
            destVC.delegate = self
        }

        if segue.identifier == "showCoffeeVC" {
            let destVC: ShowCoffeeViewController = segue.destinationViewController as! ShowCoffeeViewController

            if let selectedIndexPath = self.tableview.indexPathForSelectedRow {
                let coffee: Coffee = self.coffees[selectedIndexPath.row]
                destVC.coffeeDetail = coffee
            }

        }

    }



}




    ////////////////////////////PASSING BACK DATA

    extension CoffeeListViewController : AddCoffeeDelegate{

    func viewController(vc: AddCoffeeViewController, didAddCoffee coffee: Coffee!) {
        self.coffees.append(coffee)

        //create the cloudkit record here
        let container = CKContainer.defaultContainer()
        let publicData = container.publicCloudDatabase


        let record = CKRecord(recordType: "Coffee")
        record.setValue(coffee.name, forKey: "Name")
        record.setValue(coffee.cafe, forKey: "Cafe")
        record.setValue(coffee.rating, forKey: "Rating")
        record.setValue(coffee.place, forKey: "Place")
        publicData.saveRecord(record, completionHandler: { record, error in
            if error != nil {
                print(error)
            }
        })


        self.dismissViewControllerAnimated(true, completion: nil)
        self.tableview.reloadData()
    }
}





    ////////////////////////////////////////////////// ADPOPT TABLE VIEW

    extension CoffeeListViewController : UITableViewDelegate, UITableViewDataSource{

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

        return self.coffees.count

    }


 func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
        let cell = tableView.dequeueReusableCellWithIdentifier("CoffeeCell", forIndexPath: indexPath)

        let coffee = coffees[indexPath.row]
        cell.textLabel!.text = coffee.name
        cell.detailTextLabel!.text = "\(coffee.rating)"
        return cell
    }

}





    ////////////////////////////////////////////////// SEARCH BAR


    extension CoffeeListViewController : UISearchBarDelegate {

    func searchBarShouldEndEditing( searchBar: UISearchBar)-> Bool{
        searchBar.showsCancelButton = true
        return searchBar.showsCancelButton.boolValue
    }

    func searchBarShouldBeginEditing(searchBar: UISearchBar) -> Bool{
        searchBar.showsCancelButton = true
        return searchBar.showsCancelButton.boolValue

    }

    func searchBarCancelButtonClicked(searchBar: UISearchBar){
        searchBar.resignFirstResponder()
        searchBar.setShowsCancelButton(false, animated: false)
        call_data()


    }

    func searchBarSearchButtonClicked(searchBar: UISearchBar){
        searchBar.resignFirstResponder()
    }

    func searchBarTextDidEndEditing(searchBar: UISearchBar){

        var empty_array: [Coffee] = []

        for coffee in coffees{
            if searchBar.text?.lowercaseString == coffee.name {
                empty_array.append(coffee)
            }
        }
        self.coffees = empty_array
        self.tableview.reloadData()

        NSLog("\(searchBar.text)")


    }


}

最佳答案

您正在searchBarTextDidEndEditing上触发搜索。而是使用searchBar:textDidChange:,每次用户添加或删除字符时都会调用它。

(但是,如果您在这里使用UISearchController,那确实会更好。毕竟,这正是它的用途。)

关于ios - 使搜索功能快速响应ui搜索栏中键入的文本[swift],我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36243580/

10-11 06:41
查看更多