UITableView in XCode 8.0 (beta) Playground

In this post, I’ll be (slowly) building a UITableView application programmatically in a Playground. First we’ll add a simple UITableView, then we’ll add a UINavigationBar. In future edits, I’ll add some more views.

Add a UITableView

I adapted the following code from this GitHub gist. I adapted some of the code to Swift 3.0, and broke out the UITableViewDataSource into an extension of the UIViewController. Also, things have changed with the way Playground functionality is imported and used, and those changes are reflected in the import PlaygroundSupport statement, and the last line.

//: Playground - noun: a place where people can play

import UIKit
import PlaygroundSupport

class ViewController: UIViewController {
    var tableView: UITableView?
    let items = [ "Hello", "Hallo", "Hullo" ]
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.frame = CGRect(x: 0, y: 0, width: 320, height: 480)
        self.tableView = UITableView(frame: self.view.frame)
        self.tableView!.dataSource = self
        self.tableView!.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
        self.view.addSubview(self.tableView!)
    }
}

extension ViewController: UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.items.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        cell.textLabel?.text = "\(self.items[indexPath.row])"
        return cell
    }
}

var ctrl = ViewController()
PlaygroundPage.current.liveView = ctrl.view

Add a UINavigation Bar

I adapted the navigation bar section from a SO post. At this point, all it does is present a title and table view; I haven’t added any other views yet.

//: Playground - noun: a place where people can play

import UIKit
import PlaygroundSupport

let navBarHeight: CGFloat = 45

class ViewController: UIViewController {
    
    var tableView: UITableView?
    var navigationBar: UINavigationBar?
    
    let navTitle = "Greetings"
    let items = [ "Hello", "Hallo", "Hullo" ]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // MARK: Table View
        self.view.frame = CGRect(x: 0, y: navBarHeight, width: 320, height: 480)
        self.tableView = UITableView(frame: self.view.frame)
        self.tableView!.dataSource = self
        self.tableView!.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
        self.view.addSubview(self.tableView!)

        // MARK: Navigation Bar
        self.navigationBar = UINavigationBar(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: navBarHeight) )
        self.navigationBar!.backgroundColor = UIColor.white()
        let navigationItem = UINavigationItem(title: self.navTitle)
        self.navigationBar!.items = [navigationItem]
        self.view.addSubview(self.navigationBar!)
        
    }
}

// MARK: Table View Data Source
extension ViewController: UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.items.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        cell.textLabel?.text = "\(self.items[indexPath.row])"
        return cell
    }
}

var ctrl = ViewController()
PlaygroundPage.current.liveView = ctrl.view