本文介绍了单击导航控制器中SearchResultTableView的行后无法显示目标场景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过UISearchBar过滤条目列表,并在用户单击结果行后显示详细信息.完整列表视图和详细信息视图通过导航控制器链接.普通用例(不进行搜索)的工作原理类似于魅力:

I want to filter a list of entries via UISearchBar and show the details after user clicks a result row. Full list view and details view are linked via a navigation controller. The normal use case (without search) works like charm:

 (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)中,并带有所有条目.

I am using a UISearchBar (with UISearchDisplayController) to filter an underlying UITableView. As Apple recommends the filtered search results are displayed in the default second table view (searchDisplayController.searchResultsTableView) on top of my original table view with all entries.

一切正常-过滤条目,然后得到正确的结果行indexpath.直到用户单击搜索结果行,然后我要将所选行的详细信息视图推送到导航控制器的顶部.我的目标详细信息视图显示为BUT,但我的程序通过以下方式损坏:

Everything works fine - the entries are filtered, and I get the right result row indexpath. Until the user clicks a search result row and I want to push the details view for the selected row on top of the navigation controller. My target details view displays BUT then my program is broken in the following ways:

  1. 目标视图显示在导航栏下方(参见图像#2)
  2. 如果我在导航栏上按"BACK"(返回),则会出现一个空白屏幕(请参阅图片3),并在再次返回后单击,导致我的应用程序崩溃)uncaught exception 'NSInvalidArgumentException', reason: 'Can't add self as subview'

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

I tried displaying the target view (scene) with a segue:

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

并且我试图通过直接推送来显示目标视图:

and I tried to display the target view via direct push:

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

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

Both approaches result in the same wrong app behavior.

查看我的图片:

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

任何提示将不胜感激.

正在运行的UISearchbar:

UISearchbar in action:

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

After click on search result row - my details scene slides below the navigation bar

按"BACK"后,不显示所有条目的场景

After pressing "BACK" the scene with all entries does not display

我的故事板.注意:红色箭头标记了问题. (情节提要中的segue效果很好.但是,如果我想语法上沿红色箭头的方向走,我的UI会搞砸了!).

My storyboard. Note: The red arrow marks the problem. (The segue in the storyboard works well. But If I want to grammatically go the way of the red arrow, my UI is messed up!).

推荐答案

我找到了解决方案.

该错误非常明确.因此,很难找到其他有相同问题的人.最后,我在这里找到了一个:通过UINavigationController错误向后导航

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

由于上述SO问题,我被正确地对待了:我不得不删除tableView: didSelectRowAtIndexPath:方法的内容.

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

为什么呢?由于UISearchBar对我来说是一个新主题,因此我阅读了一些教程和一些SO问题.他们所有人都将代码放在tableView: didSelectRowAtIndexPath:中.此代码旨在处理对搜索结果表上的行的单击.参见此处: http://www.appcoda.com/how- to-add-search-bar-uitableview/和此处:向后导航UINavigationController错误

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

由于某种原因(也许是新的iOS7行为?!),此代码不再需要.更糟糕的是:此代码会产生上述问题,因为iOS7 autoall会触发搜索结果表上的相应搜索,而我的手动触发又触发了第二个搜索.源和目标相同的两个问题导致了问题.

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.

因此,我完全删除了tableView: didSelectRowAtIndexPath:覆盖方法,所有操作都像一个超级按钮!

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

这篇关于单击导航控制器中SearchResultTableView的行后无法显示目标场景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 13:55