0

我有一个UITableviewController我在 Swift4 中以编程方式创建的。在此处输入图像描述

图片中标记的矩形是我的页脚视图,UITableView里面有

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

        let tableFooterView = UITableView()
        tableFooterView.rowHeight = 100

        return tableFooterView
    }

    override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 365
    }

我想在页脚视图的表格视图单元格中加载自定义单元格。我尝试使用以下代码

 tableFooterView.dequeueReusableCell(withIdentifier: emptyCellIdentifier) as! EmptyTableViewCells

但我得到错误 -

致命错误:在展开可选值时意外发现 nil

请帮忙

4

1 回答 1

0

当您在方法中创建时,您必须为页脚 tableView 注册单元格,并且如果您正在制作委托和数据源,则viewForFooterInSection应该通过标签或其他方式区分两个表selftableFooterView UITableView

tableFooterView.register(EmptyTableViewCells.self, forCellReuseIdentifier: emptyCellIdentifier)

所以你的方法是:

func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    if tableView.tag == 1 {
        tableFooterView = UITableView()
        tableFooterView.register(EmptyTableViewCells.self, forCellReuseIdentifier: emptyCellIdentifier)
        tableFooterView.rowHeight = 100
        tableFooterView.tag = 2
        tableFooterView.dataSource = self
        tableFooterView.delegate = self
        return tableFooterView
    } else {
        //footer view is nil for the table view which is in the main table footer
        return nil
    }
}

这将停止tableFooterView.dequeueReusableCell在线崩溃,但您需要通过代码而不是 IBOutlet 在 tableFooter Empty Cell 上添加其他视图,如按钮和标签。如果您访问任何 IBOutlet 连接的组件,它将再次崩溃。

于 2017-07-02T16:43:30.287 回答