【发布时间】:2010-09-26 05:18:13
【问题描述】:
我有一个搜索UITableView 的searchDisplayController。
输入搜索词后,我可以看到另一个包含搜索结果的UITableView。但是,我希望这个 UITableView 被分组,而不是 PLAIN(默认情况下)。
我该怎么做?
【问题讨论】:
标签: iphone uisearchbar uisearchdisplaycontroller uitableview
我有一个搜索UITableView 的searchDisplayController。
输入搜索词后,我可以看到另一个包含搜索结果的UITableView。但是,我希望这个 UITableView 被分组,而不是 PLAIN(默认情况下)。
我该怎么做?
【问题讨论】:
标签: iphone uisearchbar uisearchdisplaycontroller uitableview
这对我有用(iOS 5.0):
self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
[self.searchController setValue:[NSNumber numberWithInt:UITableViewStyleGrouped]
forKey:@"_searchResultsTableViewStyle"];
【讨论】:
_searchResultsTableViewStyle 似乎没有记录在案......这会通过Apple的批准吗?
如果 - 像我一样 - 你认为普通的 TableView 太丑了,你也可以放弃使用 SearchDisplayController。
我只是:
[self.resultTableView reloadData];在这里你可以找到我从代表那里使用的所有方法
@interface MyViewController : UIViewController <UIApplicationDelegate, UISearchBarDelegate> {
IBOutlet UISearchBar *searchBar;
IBOutlet UITableView *resultTableView;
}
@property (nonatomic, retain) IBOutlet UISearchBar *searchBar;
@property (nonatomic, retain) IBOutlet UITableView *resultTableView;
//For your searchBar the 2 most important methods are
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBarClicked;
- (BOOL)searchBarTextDidEndEditing;
//For your TableView the most important methods are in my case:
//number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView;
//HEADERS
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
//different numbers of row by section
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
//the cells themselves
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
@end
毕竟,最简单的解决方案往往是最好的……
【讨论】:
这对我有用:
创建一个扩展 UISearchDisplayController 的类:
@interface RVSearchDisplayController : UISearchDisplayController
@end
@implementation RVSearchDisplayController
-(UITableView *) searchResultsTableView {
[self setValue:[NSNumber numberWithInt:UITableViewStyleGrouped]
forKey:@"_searchResultsTableViewStyle"];
return [super searchResultsTableView];
}
@end
然后使用 IB 将 UISearchDisplayController 添加到您的表中,并在 Identity Inspector 中将其自定义类更改为 RVSearchDisplayController。
【讨论】:
您可以尝试创建 UISearchDisplayController 的子类并使 searchResultsTableView 可搜索
在任何 .h 文件中添加:
@interface YourUISearchDisplayController : UISearchDisplayController {
UITableView * searchResultsTableView;
}
@property (nonatomic) UITableView * searchResultsTableView;
@end;
然后只需使用 YourUISearchDisplayController 代替 od UISearchDisplayController。
注意:您可能必须使用 (nonatomic, retain)、(nonatomic, assign) 或 (nonatomic, copy)。我不太确定
【讨论】:
-[YourUISearchDisplayController setSearchResultsTableViewStyle:]
这是不可能的,因为searchResultsTableView 属性是readonly。
【讨论】:
覆盖-searchResultsTableView 将不起作用,因为UISearchDisplayController 直接访问其表视图实例变量,而不调用该方法。
UISearchDisplayController 的指定初始化程序似乎是一个私有方法-initWithSearchBar:contentsController:searchResultsTableViewStyle:,它设置了_searchResultsTableViewStyle 实例变量。此实例变量用于创建搜索结果表视图。公共初始化程序调用此方法,并传递UITableViewStylePlain。
直接调用私有指定初始化程序或设置实例变量可能会导致应用程序被 App Store 拒绝,因此您可以尝试覆盖公共初始化程序并调用
[self setValue:[NSNumber numberWithInt:UITableViewStyleGrouped]
forKey:@"searchResultsTableViewStyle"];
【讨论】: