Skip to content

Instantly share code, notes, and snippets.

@pnicholls
Created August 2, 2014 23:09
Show Gist options
  • Save pnicholls/1341b756ee786b846104 to your computer and use it in GitHub Desktop.
Save pnicholls/1341b756ee786b846104 to your computer and use it in GitHub Desktop.
import Foundation
import UIKit
class FriendsViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var user: NDUser
var friends: [NDUser]
var fetchCompleted: Bool
lazy var tableView: UITableView = {
let tableView = UITableView()
tableView.delegate = self
tableView.dataSource = self
tableView.separatorInset = UIEdgeInsetsMake(0, 60, 0, 0)
tableView.tableFooterView = UIView(frame: CGRectZero)
tableView.registerClass(NDLoadingCell.self, forCellReuseIdentifier: "loadingCell")
tableView.registerClass(NDNoFriendsTableViewCell.self, forCellReuseIdentifier: "notFriendsCell")
tableView.registerClass(NDUserCell.self, forCellReuseIdentifier: "userCell")
tableView.reloadData()
return tableView
}()
init(user: NDUser) {
self.user = user
self.friends = []
self.fetchCompleted = false
super.init(nibName: nil, bundle: nil)
}
// MARK: View lifecycle methods
override func viewDidLoad() {
super.viewDidLoad()
self.title = "Friends"
self.view.backgroundColor = UIColor.whiteColor()
self.navigationController.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .Plain, target: nil, action: nil)
self.view.addSubview(tableView)
fetchFriends()
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.tableView.reloadData()
}
func fetchFriends() {
user.getUserFriendsWithSuccess({ (objects: [AnyObject]!) in
self.friends = objects as [NDUser]
self.tableView.reloadData()
}, failure: { (NSURLSessionDataTask, error: NSError!) in
println(error)
})
}
// MARK: UITableViewDataSource methods
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
return friends.count
}
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
let cell = tableView!.dequeueReusableCellWithIdentifier("userCell", forIndexPath: indexPath!) as NDUserCell
cell.user = friends[indexPath.row]
println(cell.user)
return cell
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment