UISearchDisplayController 文本更改滞后
UISearchDisplayController Text Did Change Lagging Behind
我正在尝试搜索我的 Parse 数据库中的用户列表。为此,我有一个搜索栏控制器和 table 视图。当用户搜索时,搜索结果似乎落后了一个字母。例如,如果我搜索 "Be" 它将显示所有以 "B" 开头的名称而不是 "Be" 并且当我搜索 "Ben" 时它显示所有以 [=18= 开头的用户].
这是我的 textDidChange 方法:
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
NSString *string = searchText;
string = string.lowercaseString;
if(string.length>0){
PFUser *currentUser = [PFUser currentUser];
PFQuery *query = [PFQuery queryWithClassName:@"_User"];
[query whereKey:@"name_lower" containsString:string];
[query whereKey:@"username" notEqualTo:currentUser.username];
[query orderByAscending:@"name_lower"];
[query setLimit:1000];
[query findObjectsInBackgroundWithBlock:^(NSArray *array,NSError *error){
if(!error){
results = [[NSArray alloc]initWithArray:array];
[_mainTableView reloadData];
}else{
[ProgressHUD showError:@"Error Searching"];
}
}];
}else{
NSLog(@"NO RESULTS");
results = nil;
[_mainTableView reloadData];
}
}
然后在我的 cellforrow 中:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell == nil) cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
PFUser *user = results[indexPath.row];
[user fetchIfNeeded];
cell.textLabel.text = [NSString stringWithFormat:@"%@ - @%@",user[@"name"],user[@"username"]];
cell.detailTextLabel.text = user[@"university"];
}
嘿,试着像这样设置约束:
[query whereKey:@"name_lower" hasPrefix:string];
而不是
[query whereKey:@"name_lower" containsString:string];
毕竟我会这样做:
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
NSString *string = searchText;
string = string.lowercaseString;
if(string.length>0){
PFQuery *query = [PFUser query];
[query whereKey:@"name_lower" hasPrefix:string];
[query whereKey:@"username" notEqualTo:[[PFUser currentUser] username]];
[query orderByAscending:@"name_lower"];
[query setLimit:1000];
[query findObjectsInBackgroundWithBlock:^(NSArray *array,NSError *error){
if(!error){
results = array;
[_mainTableView reloadData];
} else {
[ProgressHUD showError:@"Error Searching"];
}
}];
} else {
NSLog(@"NO RESULTS");
results = nil;
[_mainTableView reloadData];
}
}
我正在尝试搜索我的 Parse 数据库中的用户列表。为此,我有一个搜索栏控制器和 table 视图。当用户搜索时,搜索结果似乎落后了一个字母。例如,如果我搜索 "Be" 它将显示所有以 "B" 开头的名称而不是 "Be" 并且当我搜索 "Ben" 时它显示所有以 [=18= 开头的用户].
这是我的 textDidChange 方法:
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
NSString *string = searchText;
string = string.lowercaseString;
if(string.length>0){
PFUser *currentUser = [PFUser currentUser];
PFQuery *query = [PFQuery queryWithClassName:@"_User"];
[query whereKey:@"name_lower" containsString:string];
[query whereKey:@"username" notEqualTo:currentUser.username];
[query orderByAscending:@"name_lower"];
[query setLimit:1000];
[query findObjectsInBackgroundWithBlock:^(NSArray *array,NSError *error){
if(!error){
results = [[NSArray alloc]initWithArray:array];
[_mainTableView reloadData];
}else{
[ProgressHUD showError:@"Error Searching"];
}
}];
}else{
NSLog(@"NO RESULTS");
results = nil;
[_mainTableView reloadData];
}
}
然后在我的 cellforrow 中:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (cell == nil) cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
PFUser *user = results[indexPath.row];
[user fetchIfNeeded];
cell.textLabel.text = [NSString stringWithFormat:@"%@ - @%@",user[@"name"],user[@"username"]];
cell.detailTextLabel.text = user[@"university"];
}
嘿,试着像这样设置约束:
[query whereKey:@"name_lower" hasPrefix:string];
而不是
[query whereKey:@"name_lower" containsString:string];
毕竟我会这样做:
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
NSString *string = searchText;
string = string.lowercaseString;
if(string.length>0){
PFQuery *query = [PFUser query];
[query whereKey:@"name_lower" hasPrefix:string];
[query whereKey:@"username" notEqualTo:[[PFUser currentUser] username]];
[query orderByAscending:@"name_lower"];
[query setLimit:1000];
[query findObjectsInBackgroundWithBlock:^(NSArray *array,NSError *error){
if(!error){
results = array;
[_mainTableView reloadData];
} else {
[ProgressHUD showError:@"Error Searching"];
}
}];
} else {
NSLog(@"NO RESULTS");
results = nil;
[_mainTableView reloadData];
}
}