在 XCode 个游乐场中注册单元格重用标识符

Register cell reuse identifier within XCode Playgrounds

尝试在 XCode Playgrounds 中玩 Table 以了解它的工作原理,因为有很多东西与 UITableView 相关,一些教程描述了这些关系和幕后继承(如 UIView -> UIScrollView -> UITableView -> UITableViewCell)以及它如何管理继承、连接和委托。通常与普通导师一起,你会得到一段代码和如何在 XCode 中建立委托连接的说明以及正确的调用方法。结果 - 您记住了步骤,但仍然不了解 Table 的机制。下面是我尝试为 XCode Playgrounds 采用的代码片段,以绘制一个简单的 table。问题 - 我无法正确地以编程方式为 Table 单元格从 class 或其外部注册重用标识符。错误的性质

reason: 'unable to dequeue a cell with identifier Cell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'.

请帮助并指出错误之处和原因。

class TableViewController: UITableViewController {

let tableData = ["Matthew", "Mark", "Luke", "John"]

override func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return tableData.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as UITableViewCell
    cell.textLabel?.text = tableData[indexPath.row]
    return cell
}

override func viewDidLoad() {
    super.viewDidLoad()

    self.tableView.dataSource = self
//Did this part as comments to try to do this outside the class   
//        self.view.frame = CGRect(x: 0, y: 0, width: 320, height: 480)
//        self.tableView = UITableView(frame:self.view.frame)
//        self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
    self.view.addSubview(self.tableView)

}

}
let frame = CGRect(x: 0, y: 0, width: 320, height: 480)
let tableView = UITableView(frame: frame, style: .plain)
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
PlaygroundPage.current.liveView = TableViewController()

经过长时间尝试各种教程中构建 table 的不同解决方案后,我终于发现 UITableViewControllerUIViewController 在显示 UITableView。在我的问题示例中,有些代码行只是多余的。发生这种情况是因为我试图在 UIViewController 中实现构建 table 的方法,但我改用了 UITableViewController 并另外尝试设置数据源和所有视图属性,但正如我现在明白了——UITableViewController 已经为我做了所有这些事情(如果我错了请在这里纠正我)。功能代码片段如下:

class TableViewController: UITableViewController {

let tableData = ["Matthew", "Mark", "Luke", "John"]

  override func numberOfSections(in tableView: UITableView) -> Int {
    return 1
    }

  override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return tableData.count
    }

  override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as UITableViewCell
    cell.textLabel?.text = tableData[indexPath.row]
    return cell
    }

  override func viewDidLoad() {
    super.viewDidLoad()

    tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")

    } 
}

PlaygroundPage.current.liveView = TableViewController()

在包含 UITableView 的 UIViewController 中,以编程方式将自定义 UITableViewCell 子类注册到 UITableView 的实例是通过以下方式实现的:
- (void)registerClass:(nullable Class)cellClass forCellReuseIdentifier:(NSString *)identifier

这是注册类 应该 在设置 table 的 dataSource 之前完成。