Swift Parse 搜索栏不起作用

标签 swift parse-platform uisearchbar

我有一个国家/地区名称的解析类,并且有一个快速的表格 View Controller ,可以从 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
    }
}

}

最佳答案

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

关于Swift Parse 搜索栏不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31843389/

相关文章:

react-native - 搜索栏图标未显示 | react native

ios - 将 UISearchBar 背景更改为自定义图像的正确方法是什么?

javascript - 解析指针权限不允许创建

ios - Xcode、解析崩溃报告、未创建 dSYM 文件

swift - AVAssetImageGenerator 生成的图像颜色略有错误,但仅限于 iOS 13.X

ios - 使用 JSQMessagesViewController 时出错

ios - Parse 中的随机行

IOS:UISearchBar 部分覆盖带有提示的 UINavigationBar

ios - UITableView 复选标记重复

ios - Alamofire 4 和 JSON 中的特殊字符