CheatSheet for creating an XCode IOS app with a UITableView

Setup

  • Create a standard ViewController (NOT a TableViewController!)
  • Drag a Table onto the ViewController. Snap to edges, Reset to “Suggested Constraints”. Option Cmd Shift = is the shortcut for that.

Add the DataSource

  • Select the TableView
  • Go to the Connections Inspector
  • Click and drag the dataSource outlet to the View Controller (the yellow icon)
  • In the ViewController class: add (after UIViewController) “,UITableViewDataSource”
  • Implement these methods:
  1. cellForRowAtIndexPath (returns the value for row indexPath.row). For testing you could return a constant like this:
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
            let cell = UITableViewCell()
            cell.textLabel?.text = "My Custom Text"
            return cell
        }
  2. numberOfSectionsInTableView: return 1 or the number of sections you plan to have
  3. numberOfRowsInSection: return the number of rows you have. For testing, use 5.

Run and test the app.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.