私は小さなプロジェクトを行っていますが、問題が発生しました。UISearcBar を持つ UITableView があります。すべてが正常に機能し、検索によって正しい結果が得られますが、検索結果ごとに detailViewController に移動するために prepareForSegue メソッドを使用したいと考えています。
例えば。製品「A」を検索して見つけた場合、その製品を選択するとViewController_Aに進み、製品「B」を検索して選択すると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];
}
}
}