首页 > 解决方案 > 带有 Xib 错误的 UItableView 部分标题视图 - Swift

问题描述

我尝试使用 Xib 文件实现部分标题。我创建了一个 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 类型的未捕获异常终止

标签: swiftuitableviewxib

解决方案


Xcode 10 斯威夫特 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

    }

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

在此处输入图像描述


推荐阅读