Skip to content

Instantly share code, notes, and snippets.

@oney
Last active August 29, 2015 14:26
Show Gist options
  • Save oney/391ed921b3fb3b0ddf25 to your computer and use it in GitHub Desktop.
Save oney/391ed921b3fb3b0ddf25 to your computer and use it in GitHub Desktop.
UITableViewDataSource
In ViewController
Add a UITableView to view of ViewController
connect UITableView to tableView property of ViewController
connect dataSource of UITableView to ViewController
import UIKit
class ViewController: UIViewController, UITableViewDataSource {
var tableData: [String]!
@IBOutlet weak var tableView: UITableView?
let cellReuseIdentifier = "cellReuseIdentifier"
override func viewDidLoad() {
super.viewDidLoad()
tableView?.registerClass(UITableViewCell.self, forCellReuseIdentifier: cellReuseIdentifier)
tableData = ["foo", "bar", "kerker"]
tableView?.reloadData()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tableData!.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var string = tableData[tableData!.count - indexPath.row - 1]
var cell: UITableViewCell = tableView.dequeueReusableCellWithIdentifier(cellReuseIdentifier) as! UITableViewCell
cell.textLabel!.text = string
return cell
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment