我对iOS开发非常陌生并使用树屋构建自毁iOS应用程序,我们使用parse.com作为后端。
我在应用中添加了一个搜索栏: -
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
//dismiss keyboard and reload table
[self.searchBar resignFirstResponder];
[self.tableView reloadData];
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
//Enable the cancel button when the user touches the search field
self.searchBar.showsCancelButton = TRUE;
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {
//disable the cancel button when the user ends editing
self.searchBar.showsCancelButton = FALSE;
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
//dismiss keyboard
[self.searchBar resignFirstResponder];
//reset the foundUser property
self.foundUser = nil;
//Strip the whitespace off the end of the search text
NSString *searchText = [self.searchBar.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
//Check to make sure the field isnt empty and Query parse for username in the text field
if (![searchText isEqualToString:@""]) {
PFQuery *query = [PFUser query];
[query whereKey:@"username" equalTo:searchText];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
//check to make sure the query actually found a user
if (objects.count > 0) {
//set your foundUser property to the user that was found by the query (we use last object since its an array)
self.foundUser = objects.lastObject;
//The query was succesful but returned no results. A user was not found, display error message
} else {
}
//reload the tableView after the user searches
[self.tableView reloadData];
} else {
//error occurred with query
}
}];
}
}
当我们搜索用户时,我们必须完全正确地获取用户名,包括正确地获取大写/小写字母,然后按搜索以显示用户。
我希望用户显示即使我们没有得到正确的大写/小写字母,也要进行不区分大小写的搜索。此外,如果用户没有正确的用户名,我们可以为用户提供接近该用户名。
答案 0 :(得分:1)
您应该将用户名的全部小写值保留为PFUser类的键,或者您尝试按用户名查询的任何类。将搜索词添加到PFQuery时,请确保它全部为小写。
您可以将任何字符串转换为小写字符串,如下所示:
NSString* string = @"AAA";
NSString* lowerCaseString = string.lowercaseString;
因此,当您创建用户时,您可以执行以下操作:
PFUser* user = [PFUser user];
user.username = self.usernameTextField.lowercaseString
...
然后,当您想要查询此用户时,您的查询将类似于
...
[query whereKey:@"username" containsString:searchString.lowercaseString];
...