Skip to content

Instantly share code, notes, and snippets.

@benvium
Created August 30, 2014 19:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benvium/f6dfa10ebf5c5d5f6ed1 to your computer and use it in GitHub Desktop.
Save benvium/f6dfa10ebf5c5d5f6ed1 to your computer and use it in GitHub Desktop.
Minimal Swift TableViewController with very basic datasource from a literal array
class MyCellSubclass: UITableViewCell {
@IBOutlet var myNameLabel: UILabel!;
@IBOutlet var myOtherLabel: UILabel!;
// Do not include any inits, or we need to implement
// the required init outself
// http://stackoverflow.com/questions/25126295/swift-class-does-not-implement-its-superclasss-required-members
}
import UIKit
class ViewController: UITableViewController {
let data = [
[ "a":"foo", "b":"bar"],
[ "a":"foo2", "b":"bar2"],
[ "a":"foo3", "b":"bar3"],
[ "a":"foo4", "b":"bar4"]
];
// ! are only really there because it's an Obj-C API (implicity unwrapped optionals)
override func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
var cell = tableView.dequeueReusableCellWithIdentifier("MyCellSubclass") as MyCellSubclass;
cell.myNameLabel.text = data[indexPath.row]["a"];
cell.myOtherLabel.text = data[indexPath.row]["b"];
return cell;
}
override func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
return data.count;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment