6

我正在为我的项目实施搜索栏。搜索部分没问题。我可以显示原始数据并使用搜索文本过滤数据。当我点击搜索结果 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");
}
}

感谢帮助。

4

2 回答 2

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];
}
于 2012-08-05T23:46:36.007 回答
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];

    }
}
于 2012-09-15T09:54:57.367 回答