Swift UISearchBar Parse实现

时间:2015-01-29 00:22:31

标签: swift parse-platform uisearchbar

我很难获得我的搜索结果表格视图来更新它的内容。我试图查询的数据来自Parse。我看到它出现在我的日志中。但是,当向UISearchBar输入内容时,searchResultsTableView不会显示已过滤的结果。我错过了什么?谢谢你!

代码:

import UIKit

class DrinkSearchTableViewController: UITableViewController, UISearchBarDelegate {


var cocktails:NSMutableArray = NSMutableArray()
var filteredCocktails:NSMutableArray = NSMutableArray()


@IBOutlet var searchBar: UISearchBar! = UISearchBar()

override func viewDidLoad() {
    super.viewDidLoad()

    self.searchBar.delegate = self



}

override func viewDidAppear(animated: Bool) {
    if self.tableView == self.searchDisplayController?.searchResultsTableView {
        loadFilteredData(" ")
    } else {
        loadData()
    }
}

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

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

}

func searchBarCancelButtonClicked(searchBar: UISearchBar) {
   loadFilteredData(" ")
}

func searchBar(searchBar: UISearchBar!, textDidChange searchText: String!) {
    loadFilteredData(searchText)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

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

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

    if self.tableView == self.searchDisplayController?.searchResultsTableView {
        return filteredCocktails.count
    }
    else {
        return cocktails.count
    }

}


override func tableView(tableView: UITableView?, cellForRowAtIndexPath indexPath: NSIndexPath?) -> UITableViewCell {
    let cell = self.tableView!.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath!) as DrinkSeachTableViewCell

    if self.tableView == self.searchDisplayController?.searchResultsTableView {
        let cocktail:PFObject! = self.filteredCocktails.objectAtIndex(indexPath!.row) as PFObject
        let theCocktail = cocktail.objectForKey("Name") as String?
        cell.drinkNameLabel.text = theCocktail as String!
    } else {
        let cocktail:PFObject! = self.cocktails.objectAtIndex(indexPath!.row) as PFObject
        let theCocktail = cocktail.objectForKey("Name") as String?
        cell.drinkNameLabel.text = theCocktail as String!
    }

    return cell
}


func loadFilteredData(name:String){
    filteredCocktails.removeAllObjects()
    var query : PFQuery = PFQuery(className: "Cocktail")
    query.whereKey("Name", containsString: name)
    query.limit = 50
    query.findObjectsInBackgroundWithBlock {
        (objects: [AnyObject]!, error: NSError!) -> Void in
        if error == nil {
            for object in objects{
                let cocktail:PFObject! = object as PFObject
                println(cocktail.objectForKey("Name"))
                self.filteredCocktails.addObject(cocktail as PFObject)
                self.tableView.reloadData()
            }
        } else {

        }


    }
}



    func loadData() {
        cocktails.removeAllObjects()

        var query : PFQuery = PFQuery(className: "Cocktail")
        query.limit = 50
        query.findObjectsInBackgroundWithBlock{
            (objects:[AnyObject]!, error:NSError!) ->Void in
            if error == nil {
                // The find succeeded.
                NSLog("Successfully retrieved \(objects.count) drinks.")
                // Do something with the found objects
                for object in objects{
                    self.cocktails.addObject(object as PFObject)
                }
                self.tableView.reloadData()
            } else {
                // Log details of the failure
               println("Error getting data")
            }

        }
    }


}

1 个答案:

答案 0 :(得分:0)

尝试:
self.searchDisplayController?.searchResultsTableView.reloadData()
而不是:self.tableView.reloadData()

..在func loadFilteredData(name:String)中

和:
func searchBarSearchButtonClicked(searchBar:UISearchBar)
而不是:func searchBar(searchBar:UISearchBar!,textDidChange searchText:String!)