我正在使用filterContentForSearchText搜索表格视图。搜索效果很好但是当我选择一行时,我会将该行的ID发送到目标视图控制器。当我进行搜索时,它仍会在我搜索之前发送该行的ID。我完全理解问题是什么,但我无法弄清楚如何解决它。这就是我到目前为止所做的:
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"SELF contains[cd] %@", searchText];
searchResults = [[projectArray valueForKey:@"clientName"] filteredArrayUsingPredicate:resultPredicate];
}
和
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"CategorySegue"]) {
TimeCategoryViewController *dvc = [segue destinationViewController];
if ([self.searchDisplayController isActive]) {
NSIndexPath *path = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
NSNumber *pid = [[projectArray objectAtIndex:path.row] valueForKey:@"pid"];
dvc.selectedProject = [searchResults objectAtIndex:pid];
} else {
NSIndexPath *path = [self.tableView indexPathForSelectedRow];
NSNumber *pid = [[projectArray objectAtIndex:path.row] valueForKey:@"pid"];
dvc.selectedProject = pid;
}
}
是否有一种简单的方法可以将ID添加到搜索结果中,以便将其传递给dvc?
答案 0 :(得分:0)
问题是searchResults
仅包含每个对象的“clientName”
而不是“完整信息”。
我建议searchResults
为projectArray
的“子阵列”:
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"clientName CONTAINS[cd] %@", searchText];
searchResults = [projectArray filteredArrayUsingPredicate:resultPredicate];
}
然后,您可以访问projectArray
中的searchResults
或prepareForSegue
:
if ([self.searchDisplayController isActive]) {
NSIndexPath *path = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
NSNumber *pid = [[searchResults objectAtIndex:path.row] objectForKey:@"pid"];
dvc.selectedProject = pid;
} else {
NSIndexPath *path = [self.tableView indexPathForSelectedRow];
NSNumber *pid = [[projectArray objectAtIndex:path.row] objectForKey:@"pid"];
dvc.selectedProject = pid;
}
(当然,您还需要将cellForRowAtIndexPath
调整为此更改。)