gpt4 book ai didi

ios - 如何在 iOS 7 中实现搜索栏。搜索工作正常,但加载的结果内容与搜索结果不对应

转载 作者:可可西里 更新时间:2023-11-01 02:58:18 25 4
gpt4 key购买 nike

如何选择 TableView 的搜索结果而不是加载普通的 TableView 内容

在我的 iOS 项目中使用 UISearchBar。搜索对我来说工作正常,但在选择搜索结果行时,确实选择行方法加载原始项目而不是搜索结果行。我应该使用什么方法来加载结果行及其对应的详细信息页面。

谢谢,

代码:

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {    // Tells the table data source to reload when text changes
[self filterContentForSearchText:searchString scope:
[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:
[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];

// Return YES to cause the search result table view to be reloaded.
return YES;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self performSegueWithIdentifier:@"SubCategory" sender:self];
}

#pragma mark Content Filtering
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope {
// Update the filtered array based on the search text and scope.
// Remove all objects from the filtered search array
//[self.filteredArray removeAllObjects];
// Filter the array using NSPredicate
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@",searchText];
filteredArray=[categoryNameArray filteredArrayUsingPredicate:predicate];
}

#pragma mark - UISearchDisplayController Delegate Methods
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
// Tells the table data source to reload when text changes
[self filterContentForSearchText:searchString scope:
[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:
[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];

// Return YES to cause the search result table view to be reloaded.
return YES;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSUserDefaults *usr=[NSUserDefaults standardUserDefaults];
NSString*fontc=[usr stringForKey:@"fontchange"];
float fontSize=[fontc floatValue];
newfont= [UIFont fontWithName:@"Helvetica" size:fontSize];
static NSString *simpleTableIdentifier = @"SimpleTableCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}


if (tableView==self.searchDisplayController.searchResultsTableView)
{
cell.textLabel.text=[filteredArray objectAtIndex:indexPath.row];
} else {
cell.textLabel.text=[categoryNameArray objectAtIndex:indexPath.row];
}
cell.textLabel.font=newfont;
UIImage *img;

img = [UIImage imageNamed:@"icon_generic.png"];
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
cell.imageView.image=img;
return cell;
}

最佳答案

    - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
// Tells the table data source to reload when text changes

[self filterContentForSearchText:searchString scope:

[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:

[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];

// Return YES to cause the search result table view to be reloaded.
return YES;}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:@"SubCategory" sender:self];
}
#pragma mark Content Filtering
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope {
// Update the filtered array based on the search text and scope.

// Remove all objects from the filtered search array
//[self.filteredArray removeAllObjects];

// Filter the array using NSPredicate
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@",searchText];

filteredArray=[categoryNameArray filteredArrayUsingPredicate:predicate]; }
#pragma mark - UISearchDisplayController Delegate Methods
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
// Tells the table data source to reload when text changes

[self filterContentForSearchText:searchString scope:

[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:

[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];

// Return YES to cause the search result table view to be reloaded.
return YES;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

NSUserDefaults *usr=[NSUserDefaults standardUserDefaults];
NSString*fontc=[usr stringForKey:@"fontchange"];
float fontSize=[fontc floatValue];
newfont= [UIFont fontWithName:@"Helvetica" size:fontSize];
static NSString *simpleTableIdentifier = @"SimpleTableCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}


if (tableView==self.searchDisplayController.searchResultsTableView)
{
cell.textLabel.text=[filteredArray objectAtIndex:indexPath.row];

} else

{
cell.textLabel.text=[categoryNameArray objectAtIndex:indexPath.row];

}
cell.textLabel.font=newfont;
UIImage *img;

img = [UIImage imageNamed:@"icon_generic.png"];

cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;

cell.imageView.image=img;

return cell;}

关于ios - 如何在 iOS 7 中实现搜索栏。搜索工作正常,但加载的结果内容与搜索结果不对应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28190308/

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