Skip to content

Instantly share code, notes, and snippets.

@watert
Last active July 21, 2017 15:10
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save watert/13c38d269ea15aa8360f to your computer and use it in GitHub Desktop.
Save watert/13c38d269ea15aa8360f to your computer and use it in GitHub Desktop.
UITableView example in iOS Playground with XCode 6 beta
// Playground - noun: a place where people can play
import UIKit
class ViewController: UIViewController ,UITableViewDelegate, UITableViewDataSource
{
var tableView: UITableView!
var items: NSMutableArray!
override func viewDidLoad() {
super.viewDidLoad()
self.items = NSMutableArray(array: ["Hello 1","Hello 2","Hello 3"])
self.view.frame = CGRect(x: 0, y: 0, width: 320, height: 480)
self.tableView = UITableView(frame:self.view!.frame)
self.tableView!.delegate = self
self.tableView!.dataSource = self
self.tableView!.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
self.view?.addSubview(self.tableView)
}
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int{
return self.items.count;
}
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!{
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell
cell.textLabel.text = "\(self.items[indexPath.row])"
return cell
}
}
var ctrl = ViewController()
// ctrl.viewDidLoad() //Not needed
ctrl.view
@jpotts18
Copy link

Here is an updated version of this. This is going to make programmatic view creation a breeze. Thanks for pointing me in the right direction. 👍

import UIKit
import XCPlayground

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    var tableView: UITableView!
    let items = ["Hello 1", "Hello 2", "Hello 3"]

    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!.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
        self.view.addSubview(self.tableView)

    }
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
        return self.items.count;
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{
        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell
        cell.textLabel?.text = "\(self.items[indexPath.row])"
        return cell
    }
}

var ctrl = ViewController()
// ctrl.viewDidLoad() //Not needed
XCPShowView("Playground VC", ctrl.view)

@johngoren
Copy link

This is cool, thanks.

@JuanAntelo
Copy link

jpotts18 - what is "XCPlayground" and why did you need it?

@ginahagg
Copy link

ginahagg commented Apr 7, 2015

cool. Just what i was looking for.. XCPShowView. thanks

@codeinthesky
Copy link

in Xcode 7.1 to render the ViewController's view in the timeline (in the assistant editor) change the last line to:
XCPlaygroundPage.currentPage.liveView = ctrl.view

@klgraham
Copy link

Thanks for this!

@CrowMagic
Copy link

Thanks, this is awesome.

@codeinthesky
Copy link

Xcode 8, Swift 3 version with a delegate call back. (open the assistant editor to interact with the table view)

import UIKit
import PlaygroundSupport

class MyViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

  var tableView: UITableView!
  let items = ["Hello 1", "Hello 2", "Hello 3"]

  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!.delegate = self
    self.tableView!.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
    self.view.addSubview(self.tableView)
  }

  // DataSource
  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) as UITableViewCell
    cell.textLabel?.text = "\(self.items[indexPath.row])"
    return cell
  }

  // Delegate
  func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let alert = UIAlertController(title: "Did Select", message: "Row at index path \(indexPath)", preferredStyle: .alert)
    alert.addAction(UIAlertAction(title: "OK", style: .default))
    present(alert, animated: true)
  } 
}
var ctrl = MyViewController()
PlaygroundPage.current.liveView = ctrl.view

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment