我有一个searchDisplayController
搜索UITableView
。将searchDisplayController表视图样式更改为分组?
输入搜索条件后,我可以看到另一个包含搜索结果的UITableView
。不过,我希望这UITableView
是GROUPED,而不是PLAIN(就像默认情况下那样)。
我该怎么做?
我有一个searchDisplayController
搜索UITableView
。将searchDisplayController表视图样式更改为分组?
输入搜索条件后,我可以看到另一个包含搜索结果的UITableView
。不过,我希望这UITableView
是GROUPED,而不是PLAIN(就像默认情况下那样)。
我该怎么做?
如果 - 像我一样 - 你认为普通的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
毕竟所有的方法,最简单的解决方案往往是最好的...
这是不可能的,因为searchResultsTableView
属性是readonly
。
所以基本上是绝对没有办法将其更改为一个分组的风格?这是荒谬的,苹果会阻止我们做这样的事情.. – Noam 2010-09-26 05:27:29
@David,我知道它很烂。 :( – 2010-09-26 05:36:13
你可以尝试创建UISearchDisplayController的子类,并searchResultsTableView搜索
任何.h文件中加入:
@interface YourUISearchDisplayController : UISearchDisplayController {
UITableView * searchResultsTableView;
}
@property (nonatomic) UITableView * searchResultsTableView;
@end;
就用YourUISearchDisplayController代替OD UISearchDisplayController。注意:您可能必须使用(非原子,保留),(非原子,分配)或(非原子,复制)。我不太确定
“然后,只需用YourUISearchDisplayController代替UISearchDisplayController” 所以这是我做过什么: myUISearchDisplayController = [[MyUISearchDisplayController页头] initWithStyle:UITableViewStyleGrouped]; \t self.searchDisplayController.searchResultsTableView = myUISearchDisplayController.searchResultsTableView; 但它给我的错误,“/RootViewController.m:472:错误:对象无法设置 - 只读属性或没有设置发现 ” 有没有解决方法? – Noam 2010-09-26 07:16:38
@David,我不认为这是可能的要覆盖Objective-C中的属性,只能覆盖**方法**。 – 2010-09-26 09:32:30
是啊,我看起来你运气不好。即使你为只读属性编写setters,他们只是继续调用自己。 – 2010-09-27 17:09:56
重写-searchResultsTableView
将无法正常工作,因为UISearchDisplayController
直接访问它的表视图实例变量,而不调用方法。
UISearchDisplayController
的指定初始值设定项似乎是一个私有方法,-initWithSearchBar:contentsController:searchResultsTableViewStyle:
,它设置_searchResultsTableViewStyle
实例变量。此实例变量用于创建搜索结果表视图。公共初始化程序调用此方法,传递UITableViewStylePlain
。
直接调用私有指定的初始化或设置实例变量可能会得到从App Store拒绝的应用程序,所以你可能而是试图重写公共初始化,并呼吁
[self setValue:[NSNumber numberWithInt:UITableViewStyleGrouped]
forKey:@"searchResultsTableViewStyle"];
这个工作对我来说:
创建延伸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
。
这算作使用私人API吗? – chakrit 2012-03-27 11:25:30
这个工作对我(的iOS 5.0):
self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
[self.searchController setValue:[NSNumber numberWithInt:UITableViewStyleGrouped]
forKey:@"_searchResultsTableViewStyle"];
这是一个很好的见解,并且真正简化了搜索的使用酒吧在我的应用程序。谢谢! – Luke 2011-05-29 00:14:16