swift - 带有 Xib 错误的 UItableView 部分标题 View - Swift

标签 swift uitableview xib

我尝试使用 Xib 文件实现节标题。我创建一个 Xib View 并将 UITableViewHeaderFooterView 添加到该 View 。当我运行该应用程序时,它给我错误,没有任何描述。问题出在哪里?

提前致谢。

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 类型的未捕获异常终止

最佳答案

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

    }

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

enter image description here

关于swift - 带有 Xib 错误的 UItableView 部分标题 View - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52720146/

相关文章:

swift - layer.border 不起作用?

ios - 我在 UITableView 中混合了静态和动态 UITableViewCells,并在 numberOfRowsInSection 中得到了错误的值

ios - UIButton - NSInternalInconsistencyException - 无法使具有标识符 Cell2 的单元格出队

cocoa - 如何在 Cocoa Mac 应用程序中单击按钮打开一个新窗口?

swift - Xib 包含 NSSCrollView 时加载崩溃

ios - 快速保存高分

ios - 如何在 swift 中使用 JSONDecoder 进行类型调整?

ios - 以编程方式创建 UITableViewController 时使用 Storyboard

ios - UIButton 大小与 xib 中设置的不一样

ios - 类似于 Whatsapp 的已读状态的双复选标记