Objective-C/Xcode 6:让搜索栏填充表格视图的最佳方式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26305791/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Objective-C/Xcode 6: Best way to have a Search Bar populate a table view?
提问by H K
I have table view with a Search Bar above it. My intention is to have users enter a query in the search bar and have the table view populate with results - either when the user presses enter or as they're typing.
我有一个表格视图,上面有一个搜索栏。我的目的是让用户在搜索栏中输入查询并让表格视图填充结果 - 无论是在用户按下 Enter 键时还是在他们输入时。
After reading a number of tutorials, I selected the Search Bar and Search Display Controllerfor the Search Bar. However, it seems this controller is less of an enter-query-then-display-results tool than a filter-existing-table-view-data tool. This means I'm coming upon a table view that already has all the data and is filtered as I type -- what I'd like is to come upon an empty table view and have it populate.
在阅读了大量教程后,我为搜索栏选择了搜索栏和搜索显示控制器。然而,这个控制器似乎不是一个输入查询然后显示结果工具,而不是一个过滤器现有表视图数据工具。这意味着我会遇到一个已经包含所有数据并在我键入时进行过滤的表视图——我想要的是一个空的表视图并填充它。
I was wondering if there was a way to use the Search Bar and Search Display Controllerto achieve the effect I want or it there was a preferred way?
我想知道是否有一种方法可以使用搜索栏和搜索显示控制器来实现我想要的效果,或者有没有首选的方法?
采纳答案by ansible
When using UISearchDisplayController
you'll have two UITableView
s. The one in your search view controller. So assuming you are hooking up both UITableView
's dataSources to your UIViewController
, just check which table is being passed in and return nothing if it's not for the search.
使用时,UISearchDisplayController
您将有两个UITableView
s。搜索视图控制器中的那个。因此,假设您将两个UITableView
的数据源都连接到您的UIViewController
,只需检查传入的是哪个表,如果不是用于搜索,则不返回任何内容。
For example
例如
- (NSArray *) _sectionArrayForTable:(UITableView *) tableView {
if (tableView == self.searchDisplayController.searchResultsTableView) {
// Return your search results
}
// Show nothing when not searching
return 0;
}
回答by c_rath
These two Ray Wenderlich tutorials are great for learning how to implement search into your UITableViews.
这两个 Ray Wenderlich 教程非常适合学习如何在 UITableViews 中实现搜索。
This tutorialcovers the basic Search Bar with Objective-C. This tutorialcovers the Search Bar using Swift.
本教程涵盖了使用 Objective-C 的基本搜索栏。 本教程介绍了使用 Swift 的搜索栏。
Basically (very basic implementation level here) you will want to know if you are searching or not. If you are not searching, in your tableView:numberOfRowsInSection:
method you can return 0, otherwise return the count of the results. Then in your tableView:cellForRowAtIndexPath:
method you can customize the cell that is being displayed based upon the results.
基本上(这里是非常基本的实现级别)您会想知道您是否正在搜索。如果您不搜索,在您的tableView:numberOfRowsInSection:
方法中您可以返回 0,否则返回结果的计数。然后在您的tableView:cellForRowAtIndexPath:
方法中,您可以根据结果自定义正在显示的单元格。
回答by Zach
When the text changes, you will want to determine which results to show for that string and update some array or dictionary. Implement the tableview datasource methods to show the contents of that array/dictionary and call reload table when the text changes
当文本更改时,您将需要确定要为该字符串显示哪些结果并更新某些数组或字典。实现 tableview 数据源方法以显示该数组/字典的内容并在文本更改时调用重新加载表