使用Parse.com Swift搜索功能

使用Parse.com Swift搜索功能,swift,uitableview,parse-platform,Swift,Uitableview,Parse Platform,我试图搜索我的解析数据库中的所有用户,无论是用户名还是姓名。到目前为止,这是搜索,但不承认第一个字母。。。它只在输入第二个字母后开始搜索,一旦输入,它就会搜索第一个字母 这是我的问题,如果有人能看到这个问题,我希望你的帮助 任何更多的解释,请留下评论。。。非常感谢 func searchBar(searchBar: UISearchBar, shouldChangeTextInRange range: NSRange, replacementText text: String) -> B

我试图搜索我的解析数据库中的所有用户,无论是用户名还是姓名。到目前为止,这是搜索,但不承认第一个字母。。。它只在输入第二个字母后开始搜索,一旦输入,它就会搜索第一个字母

这是我的问题,如果有人能看到这个问题,我希望你的帮助

任何更多的解释,请留下评论。。。非常感谢

 func searchBar(searchBar: UISearchBar, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool {
    let usernameQuery = PFQuery(className: "_User")
    usernameQuery.addDescendingOrder("createdAt")
    usernameQuery.limit = 20
    usernameQuery.whereKey("username", matchesRegex: "(?i)" + self.searchBar.text!)
    usernameQuery.findObjectsInBackgroundWithBlock ({ (objects:[PFObject]?, error:NSError?) -> Void in
        if error == nil {
            if objects!.isEmpty {

                let fullnameQuery = PFUser.query()
                fullnameQuery?.whereKey("firstname", matchesRegex: "(?i)" + self.searchBar.text!)
                fullnameQuery?.findObjectsInBackgroundWithBlock({ (objects:[PFObject]?, error:NSError?) -> Void in

                    if error == nil {
                        self.search.removeAll(keepCapacity: false)
                       for object in objects! {

                            let name = SearchStruct(username: object.valueForKey("username") as! String,
                            fullname: object.valueForKey("firstname") as! String,
                            profileImage: object.valueForKey("profilePicture") as! PFFile)
                          self.search.append(name)
                        }
                        self.tableView1.reloadData()

                    }
                })
            }

            self.search.removeAll(keepCapacity: false)

            for object in objects! {
                let user = SearchStruct(username: object.valueForKey("username") as! String,
                fullname: object.valueForKey("firstname") as! String,
                profileImage: object.valueForKey("profilePicture") as! PFFile)

                self.search.append(user)
            }
            self.tableView1.reloadData()
            textLabel.text = ""
            self.activityS.stopAnimating()
        }
    })
编辑

因此,我删除了上面的代码并添加了以下内容:

 func updateSearchResultsForSearchController(searchController: UISearchController) {
       // let searchText = searchController.searchBar.text
        //Here the spinnier is initialized

        activityS.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
        activityS.startAnimating()


        let textLabel = UILabel(frame: CGRect(x: 60, y: 0, width: 300, height: 50))
        textLabel.textColor = UIColor.grayColor()
        textLabel.text = "Searching for \(searchBar.text!)"


        self.view.addSubview(activityS)
        self.view.addSubview(textLabel)


        let usernameQuery = PFQuery(className: "_User")
        usernameQuery.addDescendingOrder("createdAt")
        usernameQuery.limit = 20
        usernameQuery.whereKey("username", matchesRegex: "(?i)" + searchController.searchBar.text!)
        usernameQuery.findObjectsInBackgroundWithBlock ({ (objects:[PFObject]?, error:NSError?) -> Void in
            if error == nil {
                if objects!.isEmpty {

                    let fullnameQuery = PFUser.query()
                    fullnameQuery?.whereKey("firstname", matchesRegex: "(?i)" + searchController.searchBar.text!)
                    fullnameQuery?.findObjectsInBackgroundWithBlock({ (objects:[PFObject]?, error:NSError?) -> Void in

                        if error == nil {
                            self.search.removeAll(keepCapacity: false)
                            for object in objects! {

                                let name = SearchStruct(username: object.valueForKey("username") as! String,
                                    fullname: object.valueForKey("firstname") as! String,
                                    profileImage: object.valueForKey("profilePicture") as! PFFile)
                                self.search.append(name)
                            }
                            self.tableView1.reloadData()

                        }
                    })
                }

                self.search.removeAll(keepCapacity: false)

                for object in objects! {
                    let user = SearchStruct(username: object.valueForKey("username") as! String,
                        fullname: object.valueForKey("firstname") as! String,
                        profileImage: object.valueForKey("profilePicture") as! PFFile)

                    self.search.append(user)
                }
                self.tableView1.reloadData()
                textLabel.text = ""
                self.activityS.stopAnimating()
            }
        })
        // Do your search and refresh the table
        tableView1.reloadData()
    }

尝试使用
UISearchResultsUpdate
协议<每次在搜索栏中输入内容时,都会触发code>updateSearchResultsForSearchController

class YourTableViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, UISearchResultsUpdating, UISearchBarDelegate {

    // Initiate the searchController
    let searchController = UISearchController(searchResultsController: nil)

    override func viewDidLoad() {
        // Use the current view controller to update the search results and as delegate
        self.searchController.searchResultsUpdater = self;
        self.searchController.searchBar.delegate = self;
        self.tableView.tableHeaderView = searchController.searchBar
    }
    ...
    func updateSearchResultsForSearchController(searchController: UISearchController) {
        let searchText = searchController.searchBar.text
        // Do your search and refresh the table
        tableView.reloadData()
    }
    ...
}

希望这有帮助。

我建议下载视图中加载的所有对象。然后在用户搜索时对其进行过滤。这可能会造成严重的开销,具体取决于他有多少条条目…@penatheboss我认为这不是一个好方法,因为可能会有很多用户…@kevingoedece Yeh,这就是我这样做的主要原因。为什么不使用
UISearchResultsUpdateing
协议的
updateSearchResultsForSearchController
?您好,谢谢您的回答。。。是的,只需使用
searchController.searchBar.text
访问搜索到的文本。那么这应该是这样的:usernameQuery.whereKey(“username”,matchesRegex:(?i)+searchController.searchBar.text)?是的,我希望您实现了
UISearchBarDelegate
协议?是的,我添加了上面的代码。。但当我尝试搜索时,什么都没有发生