Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/96.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在uitableviewcontroller的第二部分中添加搜索栏_Ios_Swift - Fatal编程技术网

Ios 在uitableviewcontroller的第二部分中添加搜索栏

Ios 在uitableviewcontroller的第二部分中添加搜索栏,ios,swift,Ios,Swift,它在tableView.reloadData()上崩溃,访问权限EXC\u BAD\u。我发现这个问题是由在UIView中添加searcher引起的 那么有什么解决办法吗?我想在第二节标题中而不是第一节标题中实现搜索栏。在viewDidLoad中配置UISearchBar。 在此之后,请: class SelectionBoxViewController: UITableViewController,UISearchBarDelegate { var searchbar = UISearchBa

它在
tableView.reloadData()
上崩溃,访问权限
EXC\u BAD\u
。我发现这个问题是由在
UIView
中添加searcher引起的


那么有什么解决办法吗?我想在第二节标题中而不是第一节标题中实现搜索栏。

viewDidLoad
中配置
UISearchBar
。 在此之后,请:

class SelectionBoxViewController: UITableViewController,UISearchBarDelegate {
var searchbar = UISearchBar()

override func viewDidLoad() {
    super.viewDidLoad()
    searchbar.delegate = self
}

func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {   
    tableView.reloadData()
}  

override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
 if(section == 1){
    tableView.tableHeaderView = searchbar
        let view = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 100))
    view.addSubview(searchbar)
        searchbar.sizeToFit()
    return view
    }
    return nil
}

}

tableview的数据源在哪里?您是否正确设置了它。请查看以下链接中的UIsearchbar教程
tableHeaderView
sectionHeader
两者都不同。
override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    if(section == 1) {
       return searchBar
    } else {
       return nil
    }