I'm trying to add a search feature to my app with Parse, I have wrote the code with a UISearchController
, but for some reason when I load the app and try to search nothing works. My parse is all set up correctly, so there must be something wrong with my code!
Here it is:
import UIKit
import Parse
class SearchTableViewController: UITableViewController, UISearchResultsUpdating
{
var searchResults = [String]()
var resultSearchController = UISearchController()
override func viewDidLoad()
{super.viewDidLoad()
self.resultSearchController = UISearchController(searchResultsController: nil)
self.resultSearchController.searchResultsUpdater = self
self.resultSearchController.dimsBackgroundDuringPresentation = false
self.resultSearchController.searchBar.sizeToFit()
self.tableView.tableHeaderView = self.resultSearchController.searchBar
}
override func didReceiveMemoryWarning()
{super.didReceiveMemoryWarning()
}
func query()
{
let query = PFQuery(className:"_User")
query.findObjectsInBackgroundWithBlock { (results: [PFObject]?, error: NSError?) -> Void in
if error != nil {
if let objects = results! as [PFObject]! {
for object in objects {
let userName = object.objectForKey("firstName") as! String
self.searchResults.append(userName)
self.tableView.reloadData() //<-- must reloadData()
} } }
}
// MARK: - Table view data source
func numberOfSectionsInTableView(tableView: UITableView) -> Int
{
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return searchResults.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
cell.textLabel?.text = searchResults[indexPath.row]
return cell
}
func updateSearchResultsForSearchController(searchController: UISearchController)
{
self.searchResults.removeAll(keepCapacity: false)
let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text!)
let array = (self.searchResults as NSArray).filteredArrayUsingPredicate(searchPredicate)
self.searchResults = array as! [String]
self.tableView.reloadData()
}
}
}
first I don't think it's a good idea to filter directly from parse. It would be better to download everything from parse then you filter that data.
So your query function will now look like:
func query()
{
let query = PFQuery(className:"_User")
query.findObjectsInBackgroundWithBlock { (results: [PFObject]?, error: NSError?) -> Void in
if error != nil {
if let objects = results! as [PFObject]! {
for object in objects {
let userName = object.["firstname"] as! String
self.searchResults.append(userName)
self.tableView.reloadData() //<-- must reloadData()
} } }
}
So now your array contains firstname of all users from parse
self.tableView.reloadData() should not be in the life cycle viewDidLoad(), and don't forget to call
query()
into viewDidLoad()
you should create another array to save the result from the filters.
The numberOfRowsInSection()
method should have two options where the filter array returns filter objects from the search Bar or just objects from parse.
And the cellForRowAtIndexPath
should be also modified because you will return data from either from filter array or default array
I believed you should go trough this tutorial carefully to see how their filter code works enter link description here.