Search Bar with prepareForSegue for DetailViewController

4.3k views Asked by At

I'm implementing the search bar for my project. The search part is ok. I can display the raw data and filter the data with the search text. When I tap on the cell of the search result tableView, it didn't transition to the detail view. But for the raw data I can transition to the detail view. I use the prepareForSegue method as I'm using storyboard. Here's the code so far,

- (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];
    }
}
}

When I tried using didSelectRowAtIndexPath method, I can transition to the detail view. But I got error message like that:

  • 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>.

Here's the code for 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");
}
}

Thanks for help.

2

There are 2 answers

3
Zune Moe On

The problem is solved, Change this in cellForRowAtIndexPath

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];

to

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

and in the prepareForSegue method, you can use the self.searchDisplayController.active to check the current 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];
}
0
oscar castellon On
- (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];

    }
}