0

我正在做一个小项目,我遇到了一个问题。我有一个带有 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];
        }

    }
        }
4

1 回答 1

0

那是因为你总是调用相同的 segueId,“candyDetail”。

相反,您应该在您的 中连接两个手动 segue UIStoryBoard,每个都指向不同的场景(一个 id 为“showViewControllerA” 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...
    }
}

另一种选择是您可以将动作转场直接连接到不同的单元格,并-tableView:cellForRowAtIndexPath:根据源数组中的糖果类型切换出列的单元格类型。无论哪种方式,您都需要两个指向不同场景的转场。

于 2013-02-19T00:59:53.467 回答