Swift 在parse.com表中创建searchviewcontroller
我已经为Swift 在parse.com表中创建searchviewcontroller,swift,parse-platform,uisearchcontroller,Swift,Parse Platform,Uisearchcontroller,我已经为searchViewcontroller创建了一个基本布局,在其中输入了一个数据,当查询和搜索完成并显示单元格时,我想在解析表中搜索该数据。我尝试过搜索,但出现了各种错误。我不知道该怎么做。 我正在尝试为搜索创建一个独立的ViewController class SearchViewController: PFQueryTableViewController, UISearchBarDelegate { // Table search bar @IBOutlet weak var se
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教程。这说明了如何使用Parse实现UISearchController:我已经尝试过了,但不适用于我@Russell您在UI中实现了UISearchBar吗?你在类定义中添加了必要的子类了吗?我在故事板中添加了一个UIViewController,在那里我添加了一个UISearchBar,并创建了一个新的SearchViewController.swift我尝试了一些代码,但无法实现,因为当我开始输入关键字UIViewController崩溃后,它开始崩溃。嘿@SabhaySardana,如果您能为我们提供您编写的整个viewController,那就太好了。请使用此数据编辑问题。