gpt4 book ai didi

ios - UITableViewController 页脚内的自定义 UITableView Cell

转载 作者:行者123 更新时间:2023-11-30 12:20:20 24 4
gpt4 key购买 nike

我有一个 UITableviewController,它是我在 Swift4 中以编程方式创建的。 enter image description here

图片中标记的矩形是我的页脚 View ,里面有一个 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
}

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

 tableFooterView.dequeueReusableCell(withIdentifier: emptyCellIdentifier) as! EmptyTableViewCells

但是我收到错误 -

fatal error: unexpectedly found nil while unwrapping an Optional value

请帮忙

最佳答案

viewForFooterInSection 方法中创建时,您必须注册页脚 tableView 的单元格,并且如果您将委托(delegate)和数据源设置为 ,则应该通过标记或其他方式区分这两个表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 空单元格上添加其他 View ,例如按钮和标签。如果您访问任何 IBOutlet 连接的组件,它会再次崩溃。

关于ios - UITableViewController 页脚内的自定义 UITableView Cell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44867560/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com