我可以将UISearchBar和UILabel添加到iOS 7上的UITableView标头吗?

时间:2014-11-02 07:58:32

标签: ios uitableview uiviewcontroller uisearchbar

我有一个UITableViewController的子类。在viewDidLoad方法中,我使用下面的代码来设置表视图的标题。在viewDidAppear方法中,UI似乎就是我的意思。但是当我点击搜索栏的取消按钮时,一切都会消失。

    // header view for table view
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 100)];
    headView.backgroundColor = [UIColor redColor];

    UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
    searchBar.placeholder = @"placeholder";
    self.searchDisplayController_iOS7 = [[UISearchDisplayController alloc] initWithSearchBar:searchBar contentsController:self];
    self.searchDisplayController.delegate = self;
    self.searchDisplayController.searchResultsDelegate = self;
    self.searchDisplayController.searchResultsDataSource = self;

    [headView addSubview:searchBar];

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 50, self.view.frame.size.width, 40)];
    label.text = @"label";
    [headView addSubview:label];
    self.tableView.tableHeaderView = headView;

这是将搜索栏和标签添加到表格视图标题的正确方法吗?

0 个答案:

没有答案