As a check I have tried to simulate the error in Playground.
This was also suggested in the book. Swear that when I first tried it some time ago it worked!
The Playground produces the table in LiveView but is empty.
Here is the code:
import UIKit
import PlaygroundSupport
class TableViewExampleController: UIViewController, UITableViewDataSource {
var tableView: UITableView?
var names: [String] = ["Jane", "Akhil", "Divij"]
override func viewDidLoad() {
super.viewDidLoad()
self.view.bounds = CGRect(x: 0, y: 0, width: 375, height: 667)
createTableView()
}
func createTableView() {
self.tableView = UITableView(frame: CGRect(x: 0, y: 0, width:
self.view.frame.width, height: self.view.frame.height))
self.tableView?.dataSource = self
self.tableView?.backgroundColor = .white
self.tableView?.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
self.view.addSubview(self.tableView!)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
names.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
let name = names[indexPath.row]
cell.textLabel?.text? = name
return cell
}
}
PlaygroundPage.current.liveView = TableViewExampleController()
It seems strange that both the main project and Playground suffer from the same problem?
Is there anything in the setup or preferences that would cause this?
I don't want to reinstall Xcode as it would mean I would have to start from the beginning again.
Can anyone suggest why this behaviour is occurring?