2

How to implement Search with UISearchBar in Parse's PFQueryTableViewController,So user can search though tableview Text content.

So here is my problem so far : No result in the search bar and no crash !

Everything works well but after tapping search button adding text no result!

What seems to be the problem?

There aren't many tutorial about how to make it?

I've tried and this is my code.

ViewController

Class PFQueryTableViewController,UISearchBarDelegate


    override func queryForTable() -> PFQuery {
        var query = PFQuery(className: "Event")

       if searchBar.text != "" {
            query.whereKey("searchText", containsString: searchBar.text!.lowercaseString)
        }
        query.cachePolicy = PFCachePolicy.CacheThenNetwork
        query.orderByAscending("createdAt")
        return query  
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath, object: PFObject?) -> PFTableViewCell? {

        var cell = tableView.dequeueReusableCellWithIdentifier("EventCell") as! EventTableCell!
        if cell == nil {
            cell = EventTableCell(style: UITableViewCellStyle.Default, reuseIdentifier: "EventCell")
        }

        if let eventTitle = object!["eventTitle"] as? String {
                cell?.titleLabel?.text = eventTitle
        }
    }

///UPDATE UISearchBar Code.

func searchBarTextDidEndEditing(searchBar: UISearchBar) {


        // Dismiss the keyboard
        searchBar.resignFirstResponder()

        // Force reload of table data
        self.loadObjects()
    }
    func searchBarSearchButtonClicked(searchBar: UISearchBar) {

        // Dismiss the keyboard
        searchBar.resignFirstResponder()

        // Force reload of table data
        self.loadObjects()
    }   
    func searchBarCancelButtonClicked(searchBar: UISearchBar) {

        // Clear any search criteria
        searchBar.text = ""

        // Dismiss the keyboard
        searchBar.resignFirstResponder()

        // Force reload of table data
        self.loadObjects()
    }
     override func viewDidAppear(animated: Bool) {

        // Refresh the table to ensure any data changes are displayed
        tableView.reloadData()

        // Delegate the search bar to this table view class
        searchBar.delegate = self     
    }
user3662992
  • 688
  • 1
  • 6
  • 16
  • 1
    Try adding some `print` statements to see if the search bar text is being properly added in the query. Also here is a link to a similar question I've addressed before on implementing search with Parse: https://stackoverflow.com/questions/32356676/smart-search-for-parse-usernames-in-swift-not-working/32359180#32359180 – Russell Dec 08 '15 at 23:34
  • I added print in `func searchBarSearchButtonClicked(searchBar: UISearchBar) { print("wquwererw")` it is working – user3662992 Dec 09 '15 at 06:18
  • 1
    Add a print statement inside queryForTable. My guess is it's not being called properly – Russell Dec 09 '15 at 08:06
  • i did now & once the app runs it was printed – user3662992 Dec 09 '15 at 08:08
  • 1
    You need to be checking what the search bar text is. In particular, if it is being used properly as a query constraint – Russell Dec 09 '15 at 08:10
  • do I have to create a column inside the class for search ? – user3662992 Dec 09 '15 at 08:10
  • 1
    It looks like you already did... You query against a column called "searchText" – Russell Dec 09 '15 at 08:11
  • I don't know how to do this! I followed this tutorial it didn't works either http://swiftdeveloperblog.com/uisearchbar-example-with-swift-and-parse/ – user3662992 Dec 09 '15 at 08:13
  • Nobody likes doing someone else's work for them and it's clear that you've just copied some code without trying to understand it. Go back to the tutorials and thoroughly read them. I've seen the exact same ones and they are well written. Try to keep questions on StackOverflow specific. If you are unsure what the right question is, then you probably need to do more research beforehand – Russell Dec 09 '15 at 08:19

0 Answers0