Search bar prepared for the DetailViewController function

I am implementing a search bar for my project. Part of the search is fine. I can display raw data and filter data with search text. When I click on a cell in the table of search results, it does not go to the detailed view. But for raw data, I can move on to a detailed view. I am using the prepareForSegue method as I am using the storyboard. Here is 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 to use the didSelectRowAtIndexPath method, I can move on to the detailed view. But I got an error message:

  • 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 is 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 the help.

+6
source share
2 answers

Problem resolved, Change this in cellForRowAtIndexPath

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];

to

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

and in the prepareForSegue method you can use self.searchDisplayController.active to check the current table view

 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]; } 
+20
source
 - (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]; } } 
+1
source

Source: https://habr.com/ru/post/922141/


All Articles