在parse.com表中创建searchviewcontroller

时间:2015-12-28 13:21:47

标签: swift parse-platform uisearchcontroller

我已经为searchViewcontroller创建了一个基本布局,我在其中输入一个数据,我想在解析表中搜索一个查询并完成搜索并显示单元格。我试过搜索,但出现了各种错误。我不知道这样做。 我正在尝试创建一个独立的ViewController进行搜索。

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

}

如果提供任何线索,我将不胜感激。 此代码参考Bizzi-Body Parse教程。

0 个答案:

没有答案