Xcode 8:将UISearchBarController添加到UITableViewHeaderFooterView崩溃:nib必须只包含一个顶级对象

时间:2017-03-08 14:53:32

标签: ios xcode uitableview uisearchdisplaycontroller uitableviewsectionheader

我正在XIB文件中构建我的Section Header View:

enter image description here

这是一个XIB文件,由UISearchBarController

内的UIView组成

我在tableViewController中的viewDidLoad注册:

tableView.register(UINib(nibName: "MessagesSH", bundle: nil), forHeaderFooterViewReuseIdentifier: "MessagesSH")

我在viewForHeaderInSection:

中称呼它
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

      // Below is the where the app crashes
      let headerView = tableView.dequeueReusableHeaderFooterView(withIdentifier: "MessagesSH") as? MessagesSH 

      return headerView
    }

当我运行应用程序时,它崩溃了:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'invalid nib registered for identifier (MessagesSH) - nib must contain exactly one top level object which must be a UITableViewHeaderFooterView instance'

1 个答案:

答案 0 :(得分:0)

就我而言,我发现你不能在Xib文件中拥有任何类型的控制器(即UIViewControllerUISearchBarController等)。我不得不使用故事板而不是XIB。

相关问题