3

I have created a basic layout for searchViewcontroller in which I enter a data that I want to search inside parse table as a query and search is completed and the cell appears. I have tried searching but various errors are appearing. I have no clue to do this. I'm trying to create an independent ViewController for search.

class SearchViewController: PFQueryTableViewController, UISearchBarDelegate {

// Table search bar
@IBOutlet weak var searchBar: UISearchBar!

// Initialise the PFQueryTable tableview
override init(style: UITableViewStyle, className: String!) {
    super.init(style: style, className: className)
}

required init(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)!

    // Configure the PFQueryTableView
    self.parseClassName = "Countries"
    self.textKey = "nameEnglish"
    self.pullToRefreshEnabled = true
    self.paginationEnabled = false
}


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

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

    // Extract values from the PFObject to display in the table cell
    if let nameEnglish = object?["nameEnglish"] as? String {
        cell.customNameEnglish.text = nameEnglish
    }
    if let capital = object?["capital"] as? String {
        cell.customCapital.text = capital
    }

    // Display flag image
    var initialThumbnail = UIImage(named: "question")
    cell.customFlag.image = initialThumbnail
    if let thumbnail = object?["flag"] as? PFFile {
        cell.customFlag.file = thumbnail
        cell.customFlag.loadInBackground()
    }

    return cell
}

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    // Get the new view controller using [segue destinationViewController].
    var detailScene = segue.destinationViewController as! DetailViewController

    // Pass the selected object to the destination view controller.
    if let indexPath = self.tableView.indexPathForSelectedRow! {
        let row = Int(indexPath.row)
        detailScene.currentObject = objects?[row] as? PFObject
    }
}

// Define the query that will provide the data for the table view
override func queryForTable() -> PFQuery {

    // Start the query object
    let query = PFQuery(className: "Countries")

    // Add a where clause if there is a search criteria
    if searchBar.text != "" {
        query.whereKey("searchText", containsString: searchBar.text!.lowercaseString)
    }

    // Order the results
    query.orderByAscending("nameEnglish")

    // Return the qwuery object
    return query
}

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

}

// Override to support conditional editing of the table view.
override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
    // Return NO if you do not want the specified item to be editable.
    return true
}


// Override to support editing the table view.
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
    if editingStyle == .Delete {
        // Delete the row from the data source
        let objectToDelete = objects?[indexPath.row] as! PFObject
        objectToDelete.deleteInBackgroundWithBlock {
            (success: Bool, error: NSError?) -> Void in
            if (success) {
                // Force a reload of the table - fetching fresh data from Parse platform
                self.loadObjects()
            } else {
                // There was a problem, check error.description
            }
        }
    } else if editingStyle == .Insert {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }
   }

}

If any clue is provided I'll be grateful. This code is in reference with Bizzi-Body Parse Tutorial .

Sabhay Sardana
  • 876
  • 1
  • 10
  • 27
  • This shows how to implement a UISearchController with Parse: https://stackoverflow.com/questions/32356676/smart-search-for-parse-usernames-in-swift-not-working/32359180#32359180 – Russell Dec 28 '15 at 16:26
  • i have tried but not working for me @Russell – Sabhay Sardana Dec 28 '15 at 17:46
  • Did you implement a UISearchBar in your UI? Did you add the necessary subclasses in the class definition? – Lukesivi Jan 05 '16 at 14:34
  • I took a UIViewController in storyboard and there i added a UISearchBar and created a new SearchViewController.swift I tried few codes but I was not able to implement as it start to crash after that when i start entering the keyword, UIViewController crashes . – Sabhay Sardana Jan 06 '16 at 08:46
  • Hey @SabhaySardana, it will be just great if you provide us the whole viewController you wrote. Please edit the question with this data. – adolfosrs Jan 06 '16 at 16:54
  • Do you really want to do it with `PFQueryTableViewController` or could it be with another tableView solution that also calls a parse query? – adolfosrs Jan 06 '16 at 21:32
  • as I want an independent SearchViewController so that i think i'll need `PFQueryTableViewController` pls suggest me as the above code doesn't work for me. – Sabhay Sardana Jan 07 '16 at 15:35
  • i have implemented many searchbars with a PFQuerTableViewController in my apps and it works, i would be able to help you if you let me know what is the error you are getting....one thing i noticed is searchBarSearchButtonClicked() { after you self.loadObjects you need to call tableView.reloadData() – OverD Jan 08 '16 at 19:12
  • I'm trying to execute it again and let you know the error I'm receiving @OverD – Sabhay Sardana Jan 09 '16 at 09:40
  • Did you manage to figure out the error – OverD Jan 12 '16 at 15:05

0 Answers0