Book Image

Swift Essentials

By : Alex Blewitt, Bandlem Limited
Book Image

Swift Essentials

By: Alex Blewitt, Bandlem Limited

Overview of this book

Table of Contents (16 chapters)
Swift Essentials
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Accessing repositories from view controllers


In the MasterViewController (created from the Master Detail template; or a new subclass of a UITableViewController), define an instance variable AppDelegate that is assigned in the viewDidLoad method:

class MasterViewController:UITableViewController {
  var app:AppDelegate!
  override func viewDidLoad() {
    app = UIApplication.sharedApplication().delegate
     as? AppDelegate
    …
  }
}

The table view controller provides data in a number of sections and rows. The numberOfSections method will return the number of users, with the section title being the username (indexed by the users list):

override func numberOfSectionsInTableView(tableView: UITableView)
 -> Int {
  return app.users.count
}
override func tableView(tableView: UITableView,
 titleForHeaderInSection section: Int) -> String? {
  return app.users[section]
}

The numberOfRowsInSection function is called to determine how many rows are present in each section. If the number is not known...