我正在做一个小项目,我遇到了一个问题。我有一个带有 UISearcBar 的 UITableView。一切正常,搜索给了我正确的结果,但现在我想使用 prepareForSegue 方法来为每个搜索结果转到 detailViewController。
例如。if I search for product "A", and found it, when choose that produt it goes for a ViewController_A, if I search and chose product "B" it should go for ViewControler_B.
此刻,这段代码没有我选择的任何东西,它总是转到同一个 Viewcontroller。
#pragma mark - TableView Delegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Perform segue to candy detail
[self performSegueWithIdentifier:@"candyDetail" sender:tableView];
}
#pragma mark - Segue
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"candyDetail"]) {
UIViewController *candyDetailViewController = [segue destinationViewController];
// In order to manipulate the destination view controller, another check on which table (search or normal) is displayed is needed
if(sender == self.searchDisplayController.searchResultsTableView) {
NSIndexPath *indexPath = [self.searchDisplayController.searchResultsTableView indexPathForSelectedRow];
NSString *destinationTitle = [[filteredCandyArray objectAtIndex:[indexPath row]] name];
[candyDetailViewController setTitle:destinationTitle];
}
else {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSString *destinationTitle = [[candyArray objectAtIndex:[indexPath row]] name];
[candyDetailViewController setTitle:destinationTitle];
}
}
}