Search feature with Parse.com Swift

时间:2016-04-04 19:01:30

标签: swift uitableview parse-platform

I am trying to search all the users in my parse database, either their username or name. So far it's searches but doesn't recognize the first letter... It only starts searching after the second letter has been typed, and once it has been typed it then searches for the first letter...

Here's my query, if anyone can see the problem I'd love your help!

Any more explanation just drop a comment... Many thanks.

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

EDIT

So I removed the code above and added this:

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

1 个答案:

答案 0 :(得分:0)

尝试使用UISearchResultsUpdating协议。每次您在搜索栏中输入内容时,都会触发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()
    }
    ...
}

希望这有帮助。