Adding UISearchBar into tableView header as subview

冷暖自知 提交于 2020-01-11 09:12:08

问题


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?


回答1:


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



回答2:


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;
    }
    


来源:https://stackoverflow.com/questions/10040125/adding-uisearchbar-into-tableview-header-as-subview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!