【发布时间】:2020-02-29 13:15:50
【问题描述】:
我正在将 UITableViewController 代码移植到 SwiftUI。我已经为它创建了一个 UIViewControllerRepresentable 结构。除了以下两个之外,一切正常:
- 在 UITableViewCell 中有一个 UIView 和一个 UILabel。这两个通过插座连接到代码,它们在 UIKit 项目中完美运行。但是,当通过 SwiftUI 项目中的 UIViewControllerRepresentable 类访问时,这两者都是 nil 。我可以访问 cell.textLabel,它告诉我单元格已被初始化。
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! DepartmentTVCell
// Error: The label & cell are nil at runtime
cell.label.text = departmentsFRC?.object(at: indexPath).name
cell.view.backgroundColor = .random()
// Issue: Only the below code works
cell.textLabel?.text = departmentsFRC?.object(at: indexPath).name
return cell
}
- UIKit 项目中的 UITableViewController 不需要注册单元,因为原型单元是通过故事板连接的。但是,在 SwiftUI 版本中,单元格的自定义类只有在它被注册时才有效。没关系,但有人知道为什么会这样吗?
我已将工作项目上传到https://github.com/felixmariaa/PortTVCToSwiftUI。非常感谢您对此提供任何帮助。
谢谢。
【问题讨论】:
标签: ios swift uitableview swiftui