LoginSignup
8
11

More than 3 years have passed since last update.

PlaygroundでTable Viewを表示してみた。

Last updated at Posted at 2017-03-13

[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

SS 2017-03-13 20.02.59.png

参考

8
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
11