【问题标题】:SearchBar in table view with searchDisplayController programmatically no edit表视图中的 SearchBar 以编程方式使用 searchDisplayController 没有编辑
【发布时间】:2013-06-09 22:49:49
【问题描述】:

我正在尝试在表格的标题视图中创建一个带有搜索栏的表格视图。我想使用 searchDisplayController 来管理所有内容。 我以编程方式创建了所有内容(我对 IB 感到不舒服)试图设置所有正确的属性,但似乎我遗漏了一些东西,因为当表格出现时我无法编辑文本搜索栏并查看任何动画。 以下是部分代码:

- (void)viewDidLoad {
    [super viewDidLoad];
    UISearchBar *searchBarTMP=[[UISearchBar alloc]init];
    self.searchBar=searchBarTMP;
    [searchBarTMP release];
    self.searchBar.autocapitalizationType=UITextAutocapitalizationTypeNone;
    self.searchBar.delegate=self;
    self.searchBar.showsScopeBar=YES;
    self.searchBar.keyboardType=UIKeyboardTypeDefault;
    self.searchBar.userInteractionEnabled=YES;
    self.searchBar.multipleTouchEnabled=YES;

    self.searchBar.scopeButtonTitles=[NSArray arrayWithObjects:NSLocalizedString(@"City",@"Scope City"),NSLocalizedString(@"Postal Code",@"Scope PostalCode"),nil];
    self.tableView.tableHeaderView=searchBar;
    self.searchBar.selectedScopeButtonIndex=0;
    self.navigationItem.title=NSLocalizedString(@"Store",@"Table title");

    //SearchDisplayController creation
    UISearchDisplayController *searchDisplayControllerTMP = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];
    self.searchDisplayController=searchDisplayControllerTMP;
    [searchDisplayControllerTMP release];
    self.searchDisplayController.delegate=self;
    self.searchDisplayController.searchResultsDelegate=self;
    self.searchDisplayController.searchResultsDataSource=self;  

    //....continue
}

我知道当您单独使用搜索栏时,您必须处理它的委托协议,但我猜测 searchDisplayController 会为您管理,如 Apple 示例代码中所示。 (通过 IB 建立)。

有什么建议吗? 谢谢, 安德烈亚

【问题讨论】:

    标签: iphone uisearchbar edit uisearchdisplaycontroller


    【解决方案1】:

    找到了... 放入table view的header后一定要写

    [self.searchBar sizeToFit];
    

    【讨论】:

      【解决方案2】:

      如果您使用 ARC,请确保在头文件中为 UISearchDisplayController 创建 iVar。

      如果您使用以下方法创建 UISearchDisplayController:

      UISearchDisplayController* searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchField contentsController:self];

      它将由 ARC 释放,它不会调用任何委托方法,当您调用 self.searchDisplayController(UIViewController 的属性)时,它将是 nil

      所以,解决方法是: 在您的标头 (.h) 文件中:

      @interface MenuViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate, UISearchDisplayDelegate> {
              UISearchDisplayController* searchDisplayController;
              UISearchBar *searchField;
              UITableView* tableView;
              NSArray* searchResults;
      }
      

      在实现(.m)文件中:

      searchField = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 49)];
      searchField.delegate = self;
      
      searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:searchField contentsController:self];
      searchDisplayController.delegate = self;
      searchDisplayController.searchResultsDataSource = self;
      searchDisplayController.searchResultsDelegate = self;
      
      tableView.tableHeaderView = searchField;
      tableView.contentOffset = CGPointMake(0, searchField.frame.size.height);
      

      当这样实现时,您可以在其余代码中同时调用 self.searchDisplayControllersearchDisplayController

      【讨论】:

        猜你喜欢
        • 2017-06-18
        • 1970-01-01
        • 1970-01-01
        • 2010-09-07
        • 1970-01-01
        • 1970-01-01
        • 2020-05-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多