【问题标题】:UItableView Section Header View with Xib Error - Swift带有 Xib 错误的 UItableView 部分标题视图 - Swift
【发布时间】:2018-10-09 11:37:30
【问题描述】:

我尝试使用 Xib 文件实现 Section Header。我创建了一个 Xib 视图并参加 UITableViewHeaderFooterView 到这个视图。当我运行 App 时,它给了我没有任何描述的错误。问题出在哪里?

提前致谢。

tableView.register(UINib(nibName: "EventViewCell", bundle: nil), forCellReuseIdentifier: "EventViewCell")
tableView.register(UINib(nibName: "EventCellSectionHeader", bundle: nil), forHeaderFooterViewReuseIdentifier: "EventCellSectionHeader")


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

    let cell = tableView.dequeueReusableHeaderFooterView(withIdentifier: "EventCellSectionHeader") as! EventCellSectionHeader
    cell.headerName.text = "aa"
    return cell
}

class EventCellSectionHeader: UITableViewHeaderFooterView {
    @IBOutlet var headerName: UILabel!  
}

libc++abi.dylib:以 NSException 类型的未捕获异常终止

【问题讨论】:

  • 你能分享完整的错误信息吗?应该比你给的更多。从Terminating with uncaught exception of type NSUncaughtException(注意“Terminating”+“NSUncaughtException”与“NSException”处的大写)到你给的那个。
  • 就是这样。只需编写 libc++abi.dylib: terminating with NSException 类型的未捕获异常并重定向到 AppDelegate 文件
  • 在情节提要中检查 EventCellSectionHeader 单元格的标识符名称。
  • 我使用 xib。随便写标识符名的地方

标签: swift uitableview xib


【解决方案1】:

Xcode 10 SWIFT 4

如下实现你的 viewForHeaderInSection:

//EventCellHeader is the name of your xib file. (EventCellHeader.xib)
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

        let header = Bundle.main.loadNibNamed("EventCellHeader", owner: self, options: nil)?.last as! EventCellSectionHeader
        header.headerName.text = "aa"

        return header

    }

我测试了它:(如果您需要更多帮助,请告诉我将整个项目发送给您)

【讨论】:

  • 你的xib文件是什么类型的? UIview 什么的
  • 它是表格视图单元格。删除默认视图。
  • 你在创建swift文件时选择UITableViewHeaderFooterView?
  • 正确。整个类: 导入 UIKit 类 EventCellSectionHeader: UITableViewHeaderFooterView { @IBOutlet var headerName: UILabel! }
  • 我之前尝试过实现 UITableViewCell 并导入类名,但无法识别该名称。现在也认不出来了。但输入所有名称后点击回车,它会识别。为什么不先认出来?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-15
  • 2015-05-23
  • 2013-07-09
  • 2014-01-21
  • 1970-01-01
相关资源
最近更新 更多