如何选择表视图的搜索结果而不是加载普通的表视图内容
在我的 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;}