[2020/12/01追記]
以下のサンプルコードをXcode12.2/Swift5.3.1で動作するのを確認しました。
[追記ここまで]
Xcode8とSwift3で、PlaygroundでViewを表示して試す仕様が変わったので試してみました。
TableView.playground
import UIKit
//****************************
// MARK: Model
//****************************
struct Album {
let title: String
let artist: Artist
init(title: String, artist: Artist) {
self.title = title
self.artist = artist
}
}
struct Artist {
let name: String
}
let beatles = Artist(name: "The Beatles")
let albums = [
Album(title: "Please Please Me", artist: beatles),
Album(title: "With The Beatles", artist: beatles),
Album(title: "A Hard Day's Night", artist: beatles),
Album(title: "Beatles For Sale", artist: beatles),
Album(title: "Help!", artist: beatles),
Album(title: "Rubber Soul", artist: beatles),
Album(title: "Revolver", artist: beatles),
Album(title: "Sgt. Pepper's Lonely Hearts Club Band", artist: beatles),
Album(title: "The Beatles", artist: beatles),
Album(title: "Yellow Submarine", artist: beatles),
Album(title: "Abbey Road", artist: beatles),
Album(title: "Let it be", artist: beatles),
]
//****************************
//MARK: Table View
//****************************
final class BeatlesTableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
override func tableView(_ tableView: UITableView,
numberOfRowsInSection section: Int) -> Int {
return albums.count
}
override func tableView(_ tableView: UITableView,
heightForRowAt indexPath: IndexPath) -> CGFloat {
return 80.0
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView,
cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .subtitle, reuseIdentifier: nil)
cell.textLabel?.text = albums[indexPath.row].title
cell.detailTextLabel?.text = albums[indexPath.row].artist.name
return cell
}
}
//****************************
//MARK: Live View Configuration
//****************************
import PlaygroundSupport
// プレイグラウンドでWindowの表示
// Playgroundでの非同期処理を待つオプション
PlaygroundPage.current.needsIndefiniteExecution = true
let viewController = BeatlesTableViewController()
viewController.title = "I love Beatles."
let navigationController = UINavigationController(rootViewController: viewController)
navigationController.view.frame = CGRect(x: 0, y: 0, width: 320, height: 480)
//Live Viewに上記のViewを表示させる
PlaygroundPage.current.liveView = navigationController.view
#参考