UISearchDisplayController 和 UITableViewController
我有一个 UITableViewController,其中使用 Interface Builder 添加了一个 UISearchBar 作为 tableHeaderView。然后我在笔尖中添加了一个 UISearchDisplayController
,并设置了所有连接(delegate
、searchResultsDelegate
、searchContentsController
、 searchResultsDataSource
,全部连接到UITableViewController
)。 然后,我在代码中实现了所有委托和数据源方法。
它的工作原理就像一个魅力,除了一个奇怪的错误:有时搜索结果表视图不会滚动,我可以看到它后面的主表视图的闪烁指示器。我对 searchResultsTableView
进行了 NSLog 处理,显然它是主 tableview 的子视图,我想这就是我之前描述的触摸问题背后的原因。
我有什么错?是否可以将 UITableViewController
与 UISearchDisplayController
一起使用?如果是这样,我如何设置它,使结果表视图不会添加为主表视图的子视图?
问题是我找不到我正在做的事情和该样本正在做的事情之间有任何实质性差异。我只是在 UITableViewController
中添加一个 UISearchBar
作为 UITableView
标头,并向其中添加一个 UISearchDisplayController
。当我尝试滚动时,iOS 会混淆主表和搜索表。你有什么想法吗?
更新:添加了 200 代表赏金。 请仅在您知道自己在说什么的情况下回答。
I have a UITableViewController
, in which I added a UISearchBar as the tableHeaderView
using Interface Builder. Then I added a UISearchDisplayController
in the nib, and set up all the connections (delegate
, searchResultsDelegate
, searchContentsController
, searchResultsDataSource
, all connected to the UITableViewController
).
I then implemented all the delegate and data source methods in my code.
It works like a charm, except for a weird bug: sometimes the search results table view won't scroll, and I can see the flash indicator of the main table view behind it. I NSLog'd the searchResultsTableView
and apparently it's a subview of the main tableview, and I guess that's the reason behind the touch problems I described earlier.
What's my mistake? Is it possible to use a UITableViewController
with UISearchDisplayController
at all? If so, how do I set up it in such a way that the results table view doesn't get added as a subview of my main table view?
Update: I found this sample which uses UISearchDisplayController
with UITableViewController
and apparently the search table view gets added to the main table view in there as well. So now I don't think that's my problem.
The thing is that I can't find any substantial difference between what I'm doing and what that sample is doing. I'm just adding an UISearchBar
as a UITableView
header in a UITableViewController
and adding a UISearchDisplayController
to it... It's like iOS is confused between the main table and the search table when I try to scroll. Do you have any ideas?
Update: Added a 200 rep bounty. Please answer only if you know what you're talking about.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是一个额外的
self.tableView.scrollEnabled = YES
,搜索数据请求完成后就会调用它。希望它对将来遇到同样问题的人有所帮助。It was an extra
self.tableView.scrollEnabled = YES
which was called as soon as the search data request was finished. Hope it helps anyone with the same problem in the future.搜索显示控制器管理搜索栏和表视图的显示,该表视图显示由另一个视图控制器管理的数据的搜索结果。
这里,searchDisplaycontroller 结合了搜索栏和 tabelview,在表视图中显示结果数据,因此不需要单独的表视图。
而当你使用UISerarchBar时,它需要至少有一个UITableView,它可以在视图中显示结果。而在这里你甚至不需要添加SearchDisplayController来查看就可以看到结果。
A search display controller manages display of a search bar and a table view that displays the results of a search of data managed by another view controller.
Here the searchDisplaycontroller combines the searchbar and tabelview which showing the result data in the tableview so it won't required separate tableview.
While when you using UISerarchBar then it need to have atleast one UITableView, which can show results in the view. And here you can see the result without even adding the SearchDisplayController to view.
我想您可能想尝试将 UISearchDisplayController IBOutlet 添加到您的类中,并将其连接到 nib 文件中的 SearchdDisplayController 。我很有趣的是,笔尖中的 searchdisplay 控制器自动将其自身连接到检查器中的默认 serachdisaplaycontreoller 插座。
I think you might want to try adding a UISearchDisplayController IBOutlet to your class and connect that to your SearchdDisplayController in the nib file. I am amusing that that searchdisplay controller in your nib automatically connected it self to the default serachdisaplaycontreoller outlet in the inspector.
搜索栏控制器使用自己的表视图,该表视图添加在您自己的表视图之上。如果将 UISearchDisplayController 的委托和数据源设置为 UITableViewController,则表视图控制器必须在其委托方法中区分两个表视图。如果您没有正确执行此操作,这可能会导致您的错误。
换句话说:
您对所有委托和数据源方法执行类似的操作。
我不能 100% 确定这就是导致你的问题的原因,但在没有看到更多来源的情况下很难说。
The search bar controller uses its own table view that is added on top of your own. If you set the delegate and dataSource of the UISearchDisplayController to your UITableViewController, then the table view controller has to make a distinction between the two table views in its delegate methods. If you don't do that properly, this may be causing your bug.
In other words:
You do something similar for all of the delegate and dataSource methods.
I'm not 100% sure that this is what's causing your problem, but without seeing more source it's hard to say.