2012-08-05 80 views
6

我正在实施我的项目的搜索栏。搜索部分没问题。我可以显示原始数据并使用搜索文本过滤数据。当我点击搜索结果tableView的单元格时,它不会转换到详细视图。但对于原始数据,我可以转换到详细视图。我使用prepareForSegue方法,因为我使用的是故事板。 下面的代码到目前为止,搜索栏与prepareForSegue 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]; 
    } 
} 
} 

当我尝试使用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"); 
} 
} 

感谢您的帮助。

回答

20

问题是解决了, 变化这的cellForRowAtIndexPath

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: CellIdentifier];

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

,并在prepareForSegue方法,你可以使用self.searchDisplayController.active检查目前桌面版

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

这也给我修好了!我一直在尝试为我的第一个应用解决这个难以解决的问题,现在一周左右!感谢分享。 – 2013-06-03 07:30:23

+0

'self.searchDisplayController.active'部分保存了我的夜晚!非常感谢你。我正在学习本教程,无法使其工作:http://www.raywenderlich.com/16873/how-to-add-search-into-a-table-view您的解决方案解决了我的问题。 – scaryguy 2013-12-31 23:55:38

+0

谢谢!在我的情况下,问题是使用self.tableView而不是self.searchDisplayController.searchResultsTableView。 – Borzh 2014-10-05 20:30:20

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

    } 
}