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:
- 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 }
- numberOfSectionsInTableView: return 1 or the number of sections you plan to have
- numberOfRowsInSection: return the number of rows you have. For testing, use 5.
Run and test the app.