Created
June 15, 2016 17:37
-
-
Save charleshkang/c8818ccdc0e66ab788f9557c94ff50d5 to your computer and use it in GitHub Desktop.
This file contains 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
// | |
// MainViewController.swift | |
// Badges | |
// | |
// Created by Charles Kang on 6/14/16. | |
// Copyright © 2016 Charles Kang. All rights reserved. | |
// | |
import UIKit | |
import SwiftyJSON | |
import SDWebImage | |
class MainViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate { | |
@IBOutlet var collectionView: UICollectionView! | |
var objects = [[String: String]]() | |
override func viewDidLoad() | |
{ | |
super.viewDidLoad() | |
let khanAPI = BadgeAPI() | |
khanAPI.fetch { result in | |
switch result { | |
case .success(let objects): | |
self.objects = objects | |
case .failure(let error): | |
print(error) | |
} | |
} | |
} | |
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int | |
{ | |
return self.objects.count | |
} | |
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell | |
{ | |
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cellIdentifier", forIndexPath: indexPath) as! BadgeCollectionViewCell | |
self.collectionView.reloadData() | |
cell.layer.borderColor = UIColor.blackColor().CGColor | |
cell.layer.borderWidth = 1 | |
cell.layer.cornerRadius = 10 | |
let orderedDict = objects.sort { | |
if ($0["category"] == $1["category"]) { | |
return $0["category"] < $1["category"] | |
} else { | |
return $0["category"] < $1["category"] | |
} | |
} | |
let object = orderedDict[indexPath.row] | |
cell.badgeLabel.text = object["title"] | |
let imageURL: NSURL? = NSURL(string: object["icon"]!) | |
if let url = imageURL { | |
cell.badgeImage.sd_setImageWithURL(url) | |
} | |
return cell | |
} | |
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) | |
{ | |
print("You selected badge #\(indexPath.row)") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment