使用prepareForSegue为DetailViewController搜索栏

Zun*_*Moe 6 storyboard uisearchbar ios uisearchresultscontroller segue

我正在为我的项目实现搜索栏.搜索部分没问题.我可以显示原始数据并使用搜索文本过滤数据.当我点击搜索结果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];
    }
}
}
Run Code Online (Sandbox Code Playgroud)

当我尝试使用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");
}
}
Run Code Online (Sandbox Code Playgroud)

感谢帮助.

Zun*_*Moe 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];
}
Run Code Online (Sandbox Code Playgroud)