【问题标题】:Change the searchDisplayController table view style to grouped?将 uisearchDisplayController 表视图样式更改为分组?
【发布时间】:2010-09-26 05:18:13
【问题描述】:

我有一个搜索UITableViewsearchDisplayController

输入搜索词后,我可以看到另一个包含搜索结果的UITableView。但是,我希望这个 UITableView 被分组,而不是 PLAIN(默认情况下)。

我该怎么做?

【问题讨论】:

    标签: iphone uisearchbar uisearchdisplaycontroller uitableview


    【解决方案1】:

    这对我有用(iOS 5.0):

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

    【讨论】:

    • 这实际上是这篇文章中最好的解决方案。谢谢!
    • _searchResultsTableViewStyle 似乎没有记录在案......这会通过Apple的批准吗?
    • 我使用了这个,并且我的应用程序获得了批准。在我看来,这应该由 Apple 记录。
    • 我看不出他们有什么理由拒绝一个应用程序这样做,但危险是在未来的 iOS 版本中名称会发生​​变化,你必须更改你的应用程序才能跟上。跨度>
    【解决方案2】:

    如果 - 像我一样 - 你认为普通的 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
    

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

    【讨论】:

    • 这是一个很棒的见解,并且真正简化了我的应用程序中搜索栏的使用。谢谢!
    【解决方案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

    【讨论】:

    • 这算不算使用私有api?
    【解决方案4】:

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

    在任何 .h 文件中添加:

    @interface YourUISearchDisplayController : UISearchDisplayController {
       UITableView * searchResultsTableView;
    }
    
    @property (nonatomic) UITableView * searchResultsTableView;
    
    @end;
    

    然后只需使用 YourUISearchDisplayController 代替 od UISearchDisplayController。

    注意:您可能必须使用 (nonatomic, retain)、(nonatomic, assign) 或 (nonatomic, copy)。我不太确定

    【讨论】:

    • "那么就用 YourUISearchDisplayController 代替 UISearchDisplayController。"所以这就是我所做的: myUISearchDisplayController = [[MyUISearchDisplayController alloc] initWithStyle:UITableViewStyleGrouped]; self.searchDisplayController.searchResultsTableView = myUISearchDisplayController.searchResultsTableView;但它给我的错误是“/RootViewController.m:472: 错误: 无法设置对象 - 只读属性或未找到设置器” 有解决方法吗?
    • @David,我认为不可能覆盖 Objective-C 中的属性,只能覆盖 方法
    • 是的,看来你运气不好。事实证明,即使您为只读属性编写设置器,它们也会不断调用自己。
    • 事后想:你可以要求苹果改变它,虽然我不知道有什么可能性。
    • 您可以尝试向派生类添加独立方法,而不是尝试覆盖只读属性(您不能这样做):-[YourUISearchDisplayController setSearchResultsTableViewStyle:]
    【解决方案5】:

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

    【讨论】:

    • 所以基本上绝对没有办法将其更改为分组样式?苹果会阻止我们做这样的事情,这太荒谬了..
    【解决方案6】:

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

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

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

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

    【讨论】:

      猜你喜欢
      • 2011-01-29
      • 1970-01-01
      • 1970-01-01
      • 2020-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-08
      • 1970-01-01
      相关资源
      最近更新 更多