LoginSignup
2
1

More than 5 years have passed since last update.

Swift3+Playground でUITableViewController を表示する

Last updated at Posted at 2017-01-27

SwiftらしいTable View Controller」のコードが動かなかったので...

Swiftのバージョン
$ swift --version
Apple Swift version 3.0.2 (swiftlang-800.0.63 clang-800.0.42.1)
Target: x86_64-apple-macosx10.9

swift-playground-uitable-vc.png

import UIKit

struct Person{
    let name : String
}
let people = [
    Person(name:"たもり"),
    Person(name:"さんま"),
    Person(name:"たけし"),
]

final class MyTableViewController: UITableViewController {
    let items :[Person]
    init(items :[Person], style: UITableViewStyle) {
        self.items=items
        super.init(style:style)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return items.count
    }
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell=UITableViewCell(style:.default,reuseIdentifier:nil)
        let item=items[indexPath.row]
        cell.textLabel?.text=item.name
        return cell
    }
}

let tableVC = MyTableViewController(items:people,style:.plain)

import PlaygroundSupport

PlaygroundPage.current.liveView = tableVC
PlaygroundPage.current.needsIndefiniteExecution = true

ポイント

  1. PG用の使用モジュールが変更。
    • 旧>import XCPlayground
    • 新>import PlaygroundSupport
  2. overrideメソッド名が変更。
    • 旧>cellForRowAtIndexPath
    • 新>cellForRowAt
  3. 定数値が小文字に変更。
    • 旧>.Plain.Default
    • 新>.plain.default
2
1
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
2
1