将现有的 tableView 和 ViewController 与 UISearchDisplayController 结合使用
我有一个应用程序,通常会在整个应用程序中列出葡萄酒(全屏或较小的 UIViews)。我通常通过分配视图、将委托等分配给我的主 ViewController,然后通过该委托填充数据来调用此 ViewController。
来自想要在屏幕上显示葡萄酒的主视图:
wineListViewController = [[WineListViewController alloc] initWithWines:nil];
wineListViewController.navigationController = self.navigationController;
self.winesTableView.dataSource = wineListViewController;
self.winesTableView.delegate = wineListViewController;
// Table population code goes here
这很好,但现在我正在构建一个搜索方面,并且我想使用同一个 Viewcontroller 来显示结果,因为我有一些复杂的规则在 cellForRowAtIndexPath 等中。
然而,UISearchDisplayController 想要使用它自己的 tableView,这意味着我必须复制 WineListViewController (heightForRow,cellForRow) 中的所有代码才能以相同的方式显示。 有没有办法可以将 WineListViewController 连接到 UISearchDisplayController 中,这样我就不会重复代码?
I have an app that commonly lists Wines throughout the app (full screen or in smaller UIViews). I call this ViewController normally, by allocating the View, assigning the delegate/etc to my main ViewController, and then populating the data via that delegate.
From a master View that wants to display wines on the screen:
wineListViewController = [[WineListViewController alloc] initWithWines:nil];
wineListViewController.navigationController = self.navigationController;
self.winesTableView.dataSource = wineListViewController;
self.winesTableView.delegate = wineListViewController;
// Table population code goes here
That's great but now I'm building a Search aspect, and I want to use this same Viewcontroller for displaying the results since I have some complex rules in cellForRowAtIndexPath and such.
However, the UISearchDisplayController wants to use its own tableView, which would mean I'd have to duplicate all my code from WineListViewController (heightForRow,cellForRow) just to display the same way. Is there a way I can connect my WineListViewController into the UISearchDisplayController so I'm not duplicating code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的。
您需要将 UISearchDisplayController 的 searchResultsDataSource 和 searchResultsDelegate 设置为指向 WineListViewController 的实例(您可以在 WineListViewController 中的适当位置执行此操作并简单地传递“self”。
UISearchDisplayController 的这两个属性与 UITableView 的 dataSource 和 delegate 属性完全相同。 UISearchDisplayController 将在原始 UITableView 上呈现一个 UITableView,并使用这些属性,您可以指定应为特定搜索字符串显示哪些结果(就像原始表一样)。每次更改字符串时都应重新加载搜索结果表视图,并且将调用 UITableView 数据源和委托方法。
您可以在数据源和 WineListViewController 的委托方法中区分标准 UITableView 和 searchResultsTableView,如下所示(例如):
Y 应该查看 UISearchDisplayController 的委托方法,因为您可以实现这些方法来指定搜索显示结果的时间表应该刷新等等。
Yes.
You need to set the searchResultsDataSource and searchResultsDelegate of the UISearchDisplayController to point to the instance of your WineListViewController (you could do this in an appropriate place in the WineListViewController and simply pass 'self'.
These two properties of the UISearchDisplayController are exactly the same as the properties dataSource and delegate of a UITableView. The UISearchDisplayController is going to present a UITableView over your original UITableView, and using these properties, you can specify which results should be displayed for a particular search string (just as you would your original table). The search results table view should be reloaded every time the string is changed, and your UITableView datasource and delegate methods will be called.
You can differentiate between your standard UITableView and the searchResultsTableView in your datasource and delegate methods o the WineListViewController as follows (for example):
Y should take a look at the delegate methods of UISearchDisplayController, as you can implement those to specify when the search display results table should be refreshed and so on.