gpt4 book ai didi

ios - UISearchDisplayController 文本更改滞后

转载 作者:行者123 更新时间:2023-11-29 12:15:05 25 4
gpt4 key购买 nike

我正在尝试在我的 Parse 数据库中搜索用户列表。为此,我有一个搜索栏 Controller 和表格 View 。当用户搜索时,搜索结果似乎落后了一个字母。例如,如果我搜索“Be”,它会显示所有以“B”而不是“Be”开头的名字,当我搜索“Ben”时,它会显示所有以“Be”开头的用户。

这是我的 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"];
}

最佳答案

嘿,试着像这样设置约束:

[查询 whereKey:@"name_lower"hasPrefix:string];

代替

[查询 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];
}
}

关于ios - UISearchDisplayController 文本更改滞后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32216571/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com