我想使用 swift 和 Parse 从 PFUser 类中搜索名字和姓氏来关注和取消关注用户。我使用了 UISearchController,但当我打字时它不会检索用户。当我按下回车键时,它刚刚检索了用户。 另一个问题是,当我搜索用户时,它只检索名字而不是姓氏。例如,当我搜索 Sara John 时,它会检索到 Sara John 和 Sara March。然而,当我输入 M 时,它也检索到了 Sara March。问题是名字和姓氏一起没有被检索到!我该如何解决这个问题?
这是我的代码,我不知道我做错了什么,请帮忙。
func loadSearchUsers(searchString: String) {
let firstName = searchString
let lastName = searchString
let firstNameQuery:PFQuery = PFUser.query()!
firstNameQuery.whereKey("first_name", matchesRegex: "(?i)\(firstName)")
let lastNameQuery:PFQuery = PFUser.query()!
lastNameQuery.whereKey("last_name", matchesRegex: "(?i)\(lastName)")
let query = PFQuery.orQueryWithSubqueries([firstNameQuery, lastNameQuery])
self.searchActive = true
query.findObjectsInBackgroundWithBlock { (objects, error) -> Void in
//1
if let users = objects {
if (error != nil)
{
let myAlert = UIAlertController(title:"Error", message:error?.localizedDescription, preferredStyle:UIAlertControllerStyle.Alert)
let okAction = UIAlertAction (title: "OK", style: UIAlertActionStyle.Default, handler: nil)
myAlert.addAction(okAction)
self.presentViewController(myAlert, animated: true, completion: nil)
return
}
self.searchUsers.removeAll(keepCapacity: false)
self.userIds.removeAll(keepCapacity: false)
self.isFollowing.removeAll(keepCapacity: false)
//2
for object in users {
//3
if let user = object as? PFUser {
//4
if user.objectId! != PFUser.currentUser()?.objectId {
self.searchUsers.append(user)
self.userIds.append(user.objectId!)
self.tableView.reloadData()
let query = PFQuery(className: "followers")
query.whereKey("followFrom", equalTo: PFUser.currentUser()!.objectId!)
query.whereKey("followTo", equalTo: user.objectId!)
query.whereKey("status", equalTo:"followed")
query.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in
if let objects = objects {
if objects.count > 0 {
self.isFollowing[user.objectId!] = true
} else {
self.isFollowing[user.objectId!] = false
}
}
if self.isFollowing.count == self.userIds.count {
self.tableView.reloadData()
self.searchActive = false
}
})
}
}}
}
}
}
// MARK: - Search Bar Delegate Methods
func searchBarSearchButtonClicked(searchBar: UISearchBar) {
let searchString: String = searchBar.text!.lowercaseString
// Force search if user pushes button
if (searchString != "") {
loadSearchUsers(searchString)
}else
{loadSearchUsers("")}
self.userSearchController.resignFirstResponder()
}
func searchBarCancelButtonClicked(searchBar: UISearchBar) {
searchActive = false
searchBar.text = ""
self.resultsController.tableView.reloadData()
}
func updateSearchResultsForSearchController(searchController: UISearchController) {
let searchString = String()
if searchString.lowercaseString.containsString(self.userSearchController.searchBar.text!.lowercaseString) {
if (searchString != "" && !self.searchActive) {
loadSearchUsers(searchString)
}
//Updates the results tableview
self.resultsController.tableView.reloadData()
}
}
func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
searchActive = true
self.resultsController.tableView.reloadData()
}
最佳答案
边写边用下面的方法进行搜索
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String)
关于ios - 当我输入时 UISearchController 不检索用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35136557/