2015-01-29 2 views
0

Я изо всех сил пытаюсь получить представление таблицы результатов поиска, чтобы обновить его содержимое. Данные, которые я пытаюсь выполнить, поступают из Parse. Я вижу это в моем журнале. Однако, когда что-то вводится в UISearchBar, searchResultsTableView не показывает отфильтрованные результаты. Что мне не хватает? Спасибо вам!Swift UISearchBar Parse Implementation

Код:

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") 
      } 

     } 
    } 


} 

ответ

0

Try:
self.searchDisplayController .searchResultsTableView.reloadData()
вместо: self.tableView.reloadData()

.. в FUNC loadFilteredData (название: String)

И:
функ searchBarSearchButtonClicked (SearchBar: UISearchBar)
вместо: Func SearchBar (SearchBar: UISearchBar !, textDidChange SearchText: String)