Adding UISearchBar into tableView header as subvie

2019-02-23 07:41发布

I'm trying to add a custom header to UITableView, that has some buttons and an UISearchBar. The problem is, that when I try to use searchBar I get a message:

setting the first responder view of the table but we don't know its type (cell/header/footer)

Has anyone encounter such problem?

2条回答
戒情不戒烟
2楼-- · 2019-02-23 08:01

Just follow the simple steps here..

  1. create a property for mySearchBar in your '.h' file and synthesize.
  2. set its attributes in viewDidLoad/viewDidAppear method (or u can simply do it in the Interface Builder)
  3. Use the following delegate method to set it as the header of your table view...

    – (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {                 
            return self.mySearchBar;
    }
    
查看更多
放荡不羁爱自由
3楼-- · 2019-02-23 08:03

Are you adding to the table via:

[self.tableView addSubview:customView]

If so, that could be your error. Adding subviews to UITableView requires that you add them either as header, footer, or cell explicitly. Try:

self.tableView.tableHeaderView = customView
查看更多
登录 后发表回答