将searchDisplayController表视图样式更改为分组?

时间:2010-09-26 05:18:14

标签: iphone uisearchbar uisearchdisplaycontroller uitableview

我有searchDisplayController搜索UITableView

输入搜索字词后,我可以看到包含搜索结果的其他UITableView。但是,我希望此UITableView为GROUPED,而不是PLAIN(默认情况下为此)。

我该怎么做?

6 个答案:

答案 0 :(得分:15)

这对我有用(iOS 5.0):

self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
[self.searchController setValue:[NSNumber numberWithInt:UITableViewStyleGrouped]
        forKey:@"_searchResultsTableViewStyle"];

答案 1 :(得分:14)

如果 - 像我一样 - 你认为普通的TableView太难看了,你也可以放弃使用SearchDisplayController。

我只是:

  • 插入空白查看 searchBar TableView ,就像我们通常为IBOutlet所做的那样
  • 选择文件的所有者作为两者的委托
  • 在开始时,节的数量为0([myTab count]),然后我使用了reloadData,这次myTab由结果填充。
  • [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

毕竟,最简单的解决方案往往是最好的......

答案 2 :(得分:3)

这对我有用:

创建一个扩展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

答案 3 :(得分:1)

您可以尝试创建UISearchDisplayController的子类并使searchResultsTableView可搜索

在任何.h文件中添加:

@interface YourUISearchDisplayController : UISearchDisplayController {
   UITableView * searchResultsTableView;
}

@property (nonatomic) UITableView * searchResultsTableView;

@end;

然后只使用YourUISearchDisplayController而不是UISearchDisplayController。

注意:您可能必须使用(非原子,保留),(非原子,分配)或(非原子,复制)。我不太确定

答案 4 :(得分:0)

这是不可能的,因为searchResultsTableView属性为readonly

答案 5 :(得分:0)

覆盖-searchResultsTableView将不起作用,因为UISearchDisplayController直接访问其表视图实例变量,而不调用该方法。

UISearchDisplayController的指定初始化程序似乎是一个私有方法-initWithSearchBar:contentsController:searchResultsTableViewStyle:,它设置_searchResultsTableViewStyle实例变量。此实例变量用于创建搜索结果表视图。公共初始化程序调用此方法,传递UITableViewStylePlain

直接调用私有指定的初始化程序或设置实例变量可能会使应用程序中的应用程序被拒绝,因此您可能会尝试覆盖公共初始化程序并调用

[self setValue:[NSNumber numberWithInt:UITableViewStyleGrouped]
        forKey:@"searchResultsTableViewStyle"];