C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Example ViewController: Swift
//
//  ViewController.swift
//  ExampleTime12
//
//  ...
//
import UIKit
class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}
Protocols: Please type in UITableViewDataSource and UITableViewDelegate. Then type (or paste) the needed methods.
Methods: Add tableView—a dummy implementation can be used for the initial code. Add the second tableView also—it returns an Int.
Example UITableViewDataSource: Swift
//
//  ViewController.swift
//  ExampleTime12
//
//  ...
//
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    func tableView(tableView: UITableView,
        cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
            return UITableViewCell()
    }
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
}
Tip: A field of type UITableView will appear in your ViewController class. This will be needed for setup logic.
Note: As a performance optimization, UITableCells can be reused. This avoids many allocations and makes programs better.
Arrays: We use an "animals" array containing the names of three animals. This is displayed in the UITableView.
ArrayViewDidLoad: In this func we set the simpleTable's dataSource and delegate to the current class instance. Self means the current instance.
Cell: We use dequeueReusableCellWithIdentifier with the "Example" identifier we set for our Table Cell.
Example tableView func: Swift
//
//  ViewController.swift
//  ExampleTime12
//
//  ...
//
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
    @IBOutlet weak var simpleTable: UITableView!
    var animals = ["cat", "frog", "parrot"]
    override func viewDidLoad() {
        super.viewDidLoad()
        // Use this class as the dataSource and delegate.
        self.simpleTable.dataSource = self;
        self.simpleTable.delegate = self;
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    func tableView(tableView: UITableView,
        cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
            // Get reusable cell.
            if let cell = tableView.dequeueReusableCellWithIdentifier("Example") {
                // Set background color.
                cell.backgroundColor = UIColor.greenColor()
                // Set text of textLabel.
                // ... Use indexPath.item to get the current row index.
                if let label = cell.textLabel {
                    label.text = animals[indexPath.item]
                }
                // Return cell.
                return cell
            }
            // Return empty cell.
            return UITableViewCell()
    }
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // Use length of array a stable row count.
        return animals.count
    }
}
Optionals: We use the Swift feature that binds optionals with "if let" to access optional values. This is safe and easy.
Optional BindingUIColor: For our program we use a beautiful green color for each Table Cell, creating a professional design.
UIColorIndexPath: We use indexPath.item to get the index of each row in the first tableView func. This is an Int.
IntNumberOfRowsInSection: This returns the row count. We just use our array's count property. In this example that is equal to 3.