使用prepareForSegue为DetailViewController搜索栏

时间:2012-08-05 05:30:36

标签: ios storyboard uisearchbar segue uisearchresultscontroller

我正在为我的项目实施搜索栏。搜索部分没问题。我可以显示原始数据并使用搜索文本过滤数据。当我点击搜索结果tableView的单元格时,它没有转换到详细视图。但是对于原始数据,我可以转换到详细视图。因为我正在使用故事板,所以我使用prepareForSegue方法。 这是迄今为止的代码,

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{

    if ([segue.identifier isEqualToString: @"Book Detail Segue"]) {

    BookDetailsTVC *bookDetailsTVC = segue.destinationViewController; // for the detail view controller
    bookDetailsTVC.delegate = self;

    if (self.tableView == self.searchDisplayController.searchResultsTableView) { // The if block is not working
        NSLog(@"Search Display Controller");
        bookDetailsTVC.book = [self.searchResults objectAtIndex: self.searchDisplayController.searchResultsTableView.indexPathForSelectedRow.row];
    } else {
        NSLog(@"Default Display Controller");
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        bookDetailsTVC.book = [self.objects objectAtIndex: indexPath.row];
    }
}
}

当我尝试使用didSelectRowAtIndexPath方法时,我可以转换到详细视图。但是我收到了类似的错误消息:

  • Finishing up a navigation transition in an unexpected state. Navigation Bar subview tree might get corrupted.

  • Unbalanced calls to begin/end appearance transitions for <BookDetailsTVC: 0x69b5300>.

这是didSelectRowAtIndexPath的代码:

- (void) tableView: (UITableView *) tableView didSelectRowAtIndexPath: (NSIndexPath *) indexPath
{
    BookDetailsTVC *bookDetailsTVC = [[BookDetailsTVC alloc] init];

    if (tableView == self.searchDisplayController.searchResultsTableView) {

    bookDetailsTVC.book = [self.searchResults objectAtIndex: self.searchDisplayController.searchResultsTableView.indexPathForSelectedRow.row];

    [self performSegueWithIdentifier: @"Book Detail Segue" sender:self];

    NSLog(@"Search Display Controller");
} else {

    bookDetailsTVC.book = [self.objects objectAtIndex: indexPath.row];

    [self performSegueWithIdentifier: @"Book Detail Segue" sender: self];

    NSLog(@"Default Display Controller");
}
}

感谢您的帮助。

2 个答案:

答案 0 :(得分:20)

问题解决了, 在cellForRowAtIndexPath中更改此内容

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];

UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier: CellIdentifier];

在prepareForSegue方法中,您可以使用self.searchDisplayController.active来检查当前的tableview

if (self.searchDisplayController.active) {
    NSLog(@"Search Display Controller");
    bookDetailsTVC.book = [self.searchResults objectAtIndex: self.searchDisplayController.searchResultsTableView.indexPathForSelectedRow.row];
} else {
    NSLog(@"Default Display Controller");
    bookDetailsTVC.book = [self.objects objectAtIndex: self.tableView.indexPathForSelectedRow.row];
}

答案 1 :(得分:1)

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.searchDisplayController.searchResultsTableView) {

        self.selectedPerson = [self.searchResults objectAtIndex:indexPath.row];

        PersonasDetalle *pd = [self.storyboard instantiateViewControllerWithIdentifier:@"PersonaDetalle"];
        pd.persona = self.selectedPerson;

        [self.navigationController pushViewController:pd animated:YES];

    }
}