Created
February 20, 2015 02:33
-
-
Save ericcgu/3d18ad9ad9ec44ae49f2 to your computer and use it in GitHub Desktop.
Parse
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// JPUsersTableViewController.swift | |
// BaseballCardSocialNetwork | |
// | |
// Created by Eric Gu on 2/19/15. | |
// Copyright (c) 2015 Eric Gu. All rights reserved. | |
// | |
import UIKit | |
class JPUsersTableViewController: UITableViewController { | |
var usersArray = [PFObject]() | |
var usersPhotosArray = [UIImage]() | |
override init(style: UITableViewStyle){ | |
super.init(style: style) | |
} | |
override init(nibName nibNameOrNil: String!, bundle nibBundleOrNil: NSBundle!){ | |
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil) | |
} | |
required init(coder aDecoder: NSCoder) { | |
super.init(coder: aDecoder) | |
} | |
func loadData(){ | |
usersArray.removeAll(keepCapacity: false) | |
var findUsers = PFUser.query() | |
findUsers.whereKey("username", notEqualTo: PFUser.currentUser().username) | |
findUsers.findObjectsInBackgroundWithBlock { | |
(objects: [AnyObject]!, error: NSError!) -> Void in | |
if error == nil { | |
// Success | |
println("Successfully retrieved \(objects.count) users.") | |
// Do something with the found objects | |
for object in objects { | |
self.usersArray.append(object as PFObject) | |
} | |
self.tableView.reloadData() | |
} else { | |
// Log details of the failure | |
println(error) | |
self.presentErrorMessage(error) | |
} | |
} | |
refreshControl?.endRefreshing() | |
} | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
title = "Users" | |
configureTableView() | |
let returnIcon = UIBarButtonItem(image: kNavBarReturnIcon, style: .Plain, target: navigationController, action: "popViewControllerAnimated:") | |
returnIcon.tintColor = kToolbarIconColor | |
navigationItem.leftBarButtonItem = returnIcon | |
loadData() | |
addPullToRefresh() | |
} | |
// MARK: - Navigation | |
// MARK: - Table view data source | |
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
return usersArray.count | |
} | |
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { | |
let cell = tableView.dequeueReusableCellWithIdentifier(kTableViewCellIdentifier, forIndexPath: indexPath) as UITableViewCell | |
let user:PFObject = usersArray[indexPath.row] | |
cell.textLabel?.text = user.objectForKey("username") as String! | |
var getProfilePhotos = PFUser.query() | |
getProfilePhotos.whereKey("username", equalTo: user.objectForKey("username")) | |
getProfilePhotos.getFirstObjectInBackgroundWithBlock { (object:PFObject!, error: NSError!) -> Void in | |
if (error == nil) { | |
if let profileImage:PFFile = object["profileImage"] as? PFFile { | |
profileImage.getDataInBackgroundWithBlock({ (imageData:NSData!, error: NSError!) -> Void in | |
if (error == nil) { | |
let image:UIImage = UIImage(data: imageData)! | |
cell.imageView!.image = image | |
} | |
else{ | |
println(error) | |
self.presentErrorMessage(error) | |
} | |
}) | |
} | |
else { | |
cell.imageView!.image = kProfileDefaultProfileImage | |
} | |
} | |
else { | |
// Log details of the failure | |
println(error) | |
self.presentErrorMessage(error) | |
} | |
//self.tableView.reloadData() | |
} | |
cell.textLabel?.font = UIFont(name: kStandardFontName, size: kStandardFontSize) | |
cell.textLabel?.textColor = UIColor.whiteColor() | |
cell.backgroundColor = kbackgroundColor | |
return cell | |
} | |
func presentErrorMessage(error: NSError) { | |
// Log details of the failure | |
println(error) | |
var errorAlert = UIAlertController(title: "Oops! Something went wrong.", message: "\(error)", preferredStyle: UIAlertControllerStyle.Alert) | |
errorAlert.addAction(UIAlertAction(title: "OK", style: .Default, handler: { (action: UIAlertAction!) in | |
})) | |
self.presentViewController(errorAlert, animated: true, completion: nil) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment