我对 iOS 开发和使用 treehouse 构建自毁 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
}
}];
}
}
当我们搜索用户时,我们必须完全正确地获取用户名,包括完全正确地获取大写/小写字母,然后按搜索要显示的用户。
我希望即使我们没有得到正确的大写/小写字母,用户也能显示,所以进行不区分大小写的搜索。此外,如果用户没有获得正确的用户名,我们可以为接近该用户名的用户提供服务。
最佳答案
您应该将用户名的全小写值作为您的 PFUser 类或您尝试通过用户名查询的任何类的键。将搜索词添加到 PFQuery 时,确保它也全部小写。
您可以像这样将任何字符串转换为小写字符串:
NSString* string = @"AAA";
NSString* lowerCaseString = string.lowercaseString;
所以当你创建用户时,你会做这样的事情:
PFUser* user = [PFUser user];
user.username = self.usernameTextField.lowercaseString
...
然后当你想查询这个用户时你的查询看起来像
...
[query whereKey:@"username" containsString:searchString.lowercaseString];
...
关于ios - 搜索栏不响应 iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31786249/