gpt4 book ai didi

ios - UISearchBar 在输入文本时不显示任何结果

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:02:25 50 4
gpt4 key购买 nike

当我的用户在我的 UISearchBar 中搜索一个项目时,如果输入栏中的完整单词与其中一个结果匹配,则会显示结果。 例如 如果输入“Panda”,Panda 将在 tableView 结果中弹出。但是,如果输入“Pan”,则不会显示任何结果。如何让我的搜索结果过滤器在用户输入时起作用?即使只输入“pan”也应该显示 Panda。

我的过滤器代码目前看起来像这样:

.m

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
NSPredicate *resultPredicate = [NSPredicate
predicateWithFormat:@"SELF contains[cd] %@",
searchText];

searchResults = [self.neighbourData filteredArrayUsingPredicate:resultPredicate];
}

/*
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller
shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText:searchString
scope:[[self.searchDisplayController.searchBar scopeButtonTitles]
objectAtIndex:[self.searchDisplayController.searchBar
selectedScopeButtonIndex]]];

return YES;
} */


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

if (tableView == self.searchDisplayController.searchResultsTableView) {
return [searchResults count];

} else {

return [self.neighbourData count];

}
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

static NSString *NetworkTableIdentifier = @"sidebarCell";

self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

sidebarCell *cell = (sidebarCell *)[tableView dequeueReusableCellWithIdentifier:NetworkTableIdentifier];
if (cell == nil)

{



NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"sidebarCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}

if (tableView == self.searchDisplayController.searchResultsTableView) {



NSDictionary *userName = [searchResults objectAtIndex:indexPath.row];
[[cell username] setText:[userName objectForKey:@"first name"]];

NSDictionary *userlast = [searchResults objectAtIndex:indexPath.row];
[[cell lastName] setText:[userlast objectForKey:@"last name"]];

NSDictionary *userBio = [searchResults objectAtIndex:indexPath.row];
[[cell userDescription] setText:[userBio objectForKey:@"userbio"]];



NSString *profilePath = [[searchResults objectAtIndex:indexPath.row] objectForKey:@"photo_path"];

[cell.usermini sd_setImageWithURL:[NSURL URLWithString:profilePath]];

NSLog(@"This is profilePath %@",profilePath);



} else {

NSDictionary *userName = [self.neighbourData objectAtIndex:indexPath.row];
[[cell username] setText:[userName objectForKey:@"first name"]];

NSDictionary *userlast = [self.neighbourData objectAtIndex:indexPath.row];
[[cell lastName] setText:[userlast objectForKey:@"last name"]];

NSDictionary *userBio = [self.neighbourData objectAtIndex:indexPath.row];
[[cell userDescription] setText:[userBio objectForKey:@"userbio"]];



NSString *profilePath = [[self.neighbourData objectAtIndex:indexPath.row] objectForKey:@"photo_path"];

[cell.usermini sd_setImageWithURL:[NSURL URLWithString:profilePath]];

NSLog(@"This is profilePath %@",profilePath);




}
return cell;

}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 115;
}

邻居数据日志:

[12663:3559832] This is the neighbourdata (
{
address = "1144 fake street";
city = Las Vegas;
"first name" = Panda;
"last name" = Zoo;
"photo_path" = "none";
}

最佳答案

试试这个

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
NSPredicate * predicate =[NSPredicate predicateWithFormat:@"%K contains[cd] %@",@"first name", searchText];
searchResults = [self.neighbourData filteredArrayUsingPredicate:resultPredicate];
if searchResults.count == 0
{
NSPredicate * predicate =[NSPredicate predicateWithFormat:@"%K contains[cd] %@",@"last name",searchText];
searchResults = [self.neighbourData filteredArrayUsingPredicate:resultPredicate];
}
// add predicates for other keys also if you want

[tableView reloadData];
}

建议:

  • 避免键的单词之间有空格(不推荐 'first name',推荐 'firstName')

  • 并以全部小写形式保存值('Panda'、'Zoo' 最好保存为 'panda'、'zoo' 这将使搜索更简单)

关于ios - UISearchBar 在输入文本时不显示任何结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41113536/

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