Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search Bar with prepareForSegue for DetailViewController

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.

like image 940
Zune Moe Avatar asked Aug 05 '12 05:08

Zune Moe


2 Answers

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];
}
like image 145
Zune Moe Avatar answered Nov 23 '22 03:11

Zune Moe


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

    }
}
like image 23
oscar castellon Avatar answered Nov 23 '22 03:11

oscar castellon