2010-09-26 62 views

回答

14

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

我刚:

  • 插入一个空查看一个搜索栏的TableView就像我们平时对IBOutlet中
  • 选择文件的所有者委托他们两个做。
  • 在开始部分的数量是0([myTab计数]),然后我使用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 

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

+1

这是一个很好的见解,并且真正简化了搜索的使用酒吧在我的应用程序。谢谢! – Luke 2011-05-29 00:14:16

0

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

+0

所以基本上是绝对没有办法将其更改为一个分组的风格?这是荒谬的,苹果会阻止我们做这样的事情.. – Noam 2010-09-26 05:27:29

+0

@David,我知道它很烂。 :( – 2010-09-26 05:36:13

1

你可以尝试创建UISearchDisplayController的子类,并searchResultsTableView搜索

任何.h文件中加入:

@interface YourUISearchDisplayController : UISearchDisplayController { 
    UITableView * searchResultsTableView; 
} 

@property (nonatomic) UITableView * searchResultsTableView; 

@end; 

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

+0

“然后,只需用YourUISearchDisplayController代替UISearchDisplayController” 所以这是我做过什么: myUISearchDisplayController = [[MyUISearchDisplayController页头] initWithStyle:UITableViewStyleGrouped]; \t self.searchDisplayController.searchResultsTableView = myUISearchDisplayController.searchResultsTableView; 但它给我的错误,“/RootViewController.m:472:错误:对象无法设置 - 只读属性或没有设置发现 ” 有没有解决方法? – Noam 2010-09-26 07:16:38

+0

@David,我不认为这是可能的要覆盖Objective-C中的属性,只能覆盖**方法**。 – 2010-09-26 09:32:30

+0

是啊,我看起来你运气不好。即使你为只读属性编写setters,他们只是继续调用自己。 – 2010-09-27 17:09:56

0

重写-searchResultsTableView将无法​​正常工作,因为UISearchDisplayController直接访问它的表视图实例变量,而不调用方法。

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

直接调用私有指定的初始化或设置实例变量可能会得到从App Store拒绝的应用程序,所以你可能而是试图重写公共初始化,并呼吁

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

这个工作对我来说:

创建延伸UISearchDisplayController类:

@interface RVSearchDisplayController : UISearchDisplayController 
@end 

@implementation RVSearchDisplayController 

-(UITableView *) searchResultsTableView { 
    [self setValue:[NSNumber numberWithInt:UITableViewStyleGrouped] 
      forKey:@"_searchResultsTableViewStyle"]; 
    return [super searchResultsTableView]; 
} 

@end 

然后添加一个UISearchDisplayController添加到使用IB的表格中,并在Identity Inspector中将其定制类别更改为RVSearchDisplayController

+3

这算作使用私人API吗? – chakrit 2012-03-27 11:25:30

15

这个工作对我(的iOS 5.0):

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

这实际上是这篇文章中的最佳解决方案。谢谢! – jcdmb 2012-05-08 07:41:12

+0

这也适用于我,iOS 5.1 – jxdwinter 2012-05-15 09:46:41

+6

'_searchResultsTableViewStyle'似乎没有记录......这是否会通过苹果的批准? – 2012-09-11 07:28:58