在Swift Playgrounds for iPad中显示TableView

SRM*_*RMR 3 uitableview ios swift swift-playground

一切正常,除了我试图tableView在实时视图中显示的最后一行不起作用:PlaygroundPage.current.liveView = controller

我无法弄清楚我错了什么?

import UIKit
import PlaygroundSupport
import Foundation

class TableViewController: UITableViewController {
    let tableData = ["Matthew", "Mark", "Luke", "John"]

override func viewDidLoad() {
    super.viewDidLoad()
    print("Hello Matt")
}

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 {
    var cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as UITableViewCell
    cell.textLabel?.text = tableData[indexPath.row]
    return cell
}
}

let controller = TableViewController()
PlaygroundPage.current.liveView = controller
Run Code Online (Sandbox Code Playgroud)

Adr*_*ski 6

我想你忘记注册表视图单元类了 viewDidLoad()

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
}
Run Code Online (Sandbox Code Playgroud)