Swift Parse Searchbar不起作用

时间:2015-08-05 21:45:40

标签: swift parse-platform uisearchbar

我有一个解析类的国家/地区名称,并且有一个快速的tableview控制器可以从Parse中提取国家/地区列表并允许您搜索国家/地区名称。例如: 加拿大 中国 伊拉克 等

如果我搜索“加拿大”没有出现,但如果我搜索“anada”,我会得到结果。出于某种原因,它忽略了第一个角色,不知道为什么会发生这种情况?

import UIKit

class TableViewController: PFQueryTableViewController, UISearchBarDelegate {

// Sign the user out
@IBAction func signOut(sender: AnyObject) {

    PFUser.logOut()

    let storyboard = UIStoryboard(name: "Main", bundle: nil)
    let vc = storyboard.instantiateViewControllerWithIdentifier("SignUpInViewController") as! UIViewController
    self.presentViewController(vc, animated: true, completion: nil)
}

@IBAction func add(sender: AnyObject) {

    dispatch_async(dispatch_get_main_queue()) {
        self.performSegueWithIdentifier("TableViewToDetailView", sender: self)
    }
}

// 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
    var query = PFQuery(className: "Countries")

    // Add a where clause if there is a search criteria
    if searchBar.text != "" {
        query.whereKey("nameEnglish", 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
    }
}

}

1 个答案:

答案 0 :(得分:0)

刚刚意识到query.wherekey函数不区分大小写,修复了问题