Skip to content

Instantly share code, notes, and snippets.

@welbesw
Created June 7, 2016 18:22
Show Gist options
  • Save welbesw/a52ea6e5880222fff11ca1f7fdc81e0d to your computer and use it in GitHub Desktop.
Save welbesw/a52ea6e5880222fff11ca1f7fdc81e0d to your computer and use it in GitHub Desktop.
class ContactsViewController: UITableViewController {
private var contacts:[SalesforceContact] = []
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
reloadContacts()
}
// MARK: - Table view data source
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// return the number of sections
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// return the number of rows
return contacts.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("contactCell", forIndexPath: indexPath) as! ContactCell
// Configure the cell...
let contact = contacts[indexPath.row]
let firstName = contact.firstName != nil ? contact.firstName! : ""
let lastName = contact.lastName != nil ? contact.lastName! : ""
cell.nameLabel.text = "\(firstName) \(lastName)"
cell.emailLabel.text = (contact.email ?? "").isEmpty ? "" : contact.email!
return cell
}
@IBAction func refreshContacts(sender: UIRefreshControl) {
reloadContacts()
}
func reloadContacts() {
SalesforceManager.sharedInstance.fetchContacts { (contacts, error) in
dispatch_async(dispatch_get_main_queue(), {
self.refreshControl?.endRefreshing()
if error == nil {
self.contacts = contacts
self.tableView.reloadData()
} else {
let alertController = UIAlertController(title: "Error Loading Contacts", message: error!.localizedDescription, preferredStyle: .Alert)
self.presentViewController(alertController, animated: true, completion: nil)
}
})
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment