0

我想通过 UISearchBar 过滤条目列表,并在用户单击结果行后显示详细信息。完整列表视图和详细信息视图通过导航控制器链接。正常用例(没有搜索)就像魅力一样:

 (ListOfAllEntries)
 => (direct click on row)
 ==> (Details view for row)

这也应该起作用:

(ListOfAllEntries) 
=> (Search)                  - OK!
==> (ListOfFilteredEntries)  - OK!
===> (click on result row)          - OK!
====> (Details view for row) - BOOUUMMM! UI and Nav.Ctrl broken

我正在使用 UISearchBar(带有 UISearchDisplayController)来过滤底层 UITableView。正如 Apple 建议的那样,过滤后的搜索结果显示在我的原始表视图顶部的默认第二个表视图 (searchDisplayController.searchResultsTableView) 中,其中包含所有条目。

一切正常 - 条目被过滤,我得到了正确的结果行索引路径。直到用户单击搜索结果行并且我想将所选行的详细信息视图推送到导航控制器的顶部。我的目标详细信息视图显示但我的程序以下列方式被破坏:

  1. 目标视图显示在导航栏下方(参见图片#2)
  2. 如果我在导航栏上按“BACK”,我会看到一个空白屏幕(参见图片#3),然后再点击我的应用程序崩溃)uncaught exception 'NSInvalidArgumentException', reason: 'Can't add self as subview'

我尝试使用 segue 显示目标视图(场景):

if (tableView == self.searchController.searchResultsTableView) {
    [self performSegueWithIdentifier: @"showFoods" sender: self];
}

我尝试通过直接推送显示目标视图:

FoodViewController *fvc = [self.storyboard instantiateViewControllerWithIdentifier:@"FoodViewController"];
[self.navigationController pushViewController:fvc animated:YES];

两种方法都会导致相同的错误应用行为。

看我的图片:

  • 为什么我的详细信息列表在导航栏下方?
  • 为什么我推送我的细节场景后导航堆栈会乱码?

任何提示将不胜感激。

UISearchbar 在行动:

UISearchbar 在行动

单击搜索结果行后 - 我的详细信息场景在导航栏下方滑动

单击搜索结果行后 - 我的详细信息场景在导航栏下方滑动

按“BACK”后,所有条目的场景都不会显示

按下后

我的故事板。注意:红色箭头标记了问题。(故事板中的转场效果很好。但如果我想在语法上按照红色箭头的方式走,我的 UI 就搞砸了!)。

红色箭头方式弄乱了用户界面

4

1 回答 1

0

I found the solution.

The error is very unspecific. And thus other people with the same problem are hard to find. Finally I found one here: Navigating Backwards through a UINavigationController Error

Thanks to the above SO question I was put on the right track: I had to delete the content of my tableView: didSelectRowAtIndexPath: method.

Why that? As UISearchBar was a new topic for me, I read some tutorials and some SO questions. And all of them had put code inside tableView: didSelectRowAtIndexPath:. This code was intended to handle the click on a row on the search result table. See here: http://www.appcoda.com/how-to-add-search-bar-uitableview/ and here: Navigating Backwards through a UINavigationController Error

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{    
    if (tableView == self.searchDisplayController.searchResultsTableView) {
        [self performSegueWithIdentifier: @"showRecipeDetail" sender: self];        
    }
}

For some reason, (maybe new iOS7 behaviour?!), this code is not necessary anymore. Even worse: this code produces the above problems, as iOS7 automaticall triggers the according segue on the search result table and my manuall trigger made a second segue to fire off. Two segues with the same source and target had caused the problems.

So I completely deleted my tableView: didSelectRowAtIndexPath: overwrite method and everything works like a charm!

于 2013-12-29T17:05:29.947 回答