0

私は小さなプロジェクトを行っていますが、問題が発生しました。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];
        }

    }
        }
4

1 に答える 1

0

これは、常に同じ segueId である「candyDetail」を呼び出しているためです。

代わりに、2 つの手動セグエを に接続しUIStoryBoard、それぞれが異なるシーンを指している必要があります (1 つは「showViewControllerA」の ID を持ち、もう 1 つは をViewControllerA指す「showViewControllerB」ViewControllerBです)。次に、次のことができます。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([[self.candyArray objectAtIndex:indexPath.row] isKindOfClass:[CandyA class]]) {
        [self performSegueWithIdentifier:@"showViewControllerA" sender:self];
    } else if ([[self.candyArray objectAtIndex:indexPath.row] isKindOfClass:[CandyB class]]) {
        [self performSegueWithIdentifier:@"showViewControllerB" sender:self];
    };
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"showViewControllerA"]) {
        ViewControllerA *viewControllerA = [segue destinationViewController];
        // configure viewControllerA here...
    } else if ([[segue identifier] isEqualToString:@"showViewControllerA"]) {
        ViewControllerB *viewControllerB = [segue destinationViewController];
        // configure viewControllerB here...
    }
}

もう 1 つの方法は、アクション セグエを別のセルに直接接続し-tableView:cellForRowAtIndexPath:、ソース配列のキャンディー タイプに基づいてデキューするセル タイプを切り替えることです。いずれにせよ、異なるシーンを指す 2 つのセグエが必要です。

于 2013-02-19T00:59:53.467 に答える