2012-08-05 13 views
6

Estoy implementando la barra de búsqueda para mi proyecto. La parte de búsqueda está bien. Puedo mostrar los datos brutos y filtrar los datos con el texto de búsqueda. Cuando toco en la celda del resultado de búsqueda tableView, no hizo la transición a la vista de detalles. Pero para los datos brutos, puedo pasar a la vista de detalles. Uso el método prepareForSegue porque estoy usando el guión gráfico. Aquí está el código hasta el momento,Barra de búsqueda con prepareForSegue para DetailViewController

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

Cuando intenté usar didSelectRowAtIndexPath método, que puede pasar a la vista de detalle. Pero llegué mensaje de error como que:

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

Aquí está el código para 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"); 
} 
} 

Gracias por la ayuda.

Respuesta

20

El problema se resuelve, cambiar esto en cellForRowAtIndexPath

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];

a

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

y en el método prepareForSegue, se puede utilizar el self.searchDisplayController.active para comprobar la tabla de vista actual

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

¡Eso lo solucionó también! He estado tratando de resolver este problema difícil de alcanzar para mi primera aplicación durante una semana más o menos ahora! Gracias por compartir. –

+0

'self.searchDisplayController.active' parte me ha salvado la noche !! Muchas gracias. Estaba siguiendo este tutorial y no pude hacer que funcione: http://www.raywenderlich.com/16873/how-to-add-search-into-a-table-view tu solución resolvió mi problema. – scaryguy

+0

Gracias! En mi caso, el problema fue usar self.tableView en lugar de self.searchDisplayController.searchResultsTableView. – Borzh

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

    } 
} 
Cuestiones relacionadas