ViewController.swift 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. //
  2. // ViewController.swift
  3. // AFNetworking tvOS Example
  4. //
  5. // Created by Kevin Harwood on 9/24/15.
  6. // Copyright © 2015 Alamofire. All rights reserved.
  7. //
  8. import UIKit
  9. class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
  10. @IBOutlet var collectionView: UICollectionView!
  11. var gravatars: [Gravatar] = []
  12. override func viewDidLoad() {
  13. super.viewDidLoad()
  14. for _ in 1...100 {
  15. let gravatar = Gravatar(
  16. emailAddress: NSUUID().UUIDString,
  17. defaultImage: Gravatar.DefaultImage.Identicon,
  18. forceDefault: true
  19. )
  20. gravatars.append(gravatar)
  21. }
  22. }
  23. override func didReceiveMemoryWarning() {
  24. super.didReceiveMemoryWarning()
  25. // Dispose of any resources that can be recreated.
  26. }
  27. func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  28. return gravatars.count
  29. }
  30. func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
  31. return 1
  32. }
  33. func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
  34. let cell = collectionView.dequeueReusableCellWithReuseIdentifier("CollectionViewCell", forIndexPath: indexPath) as! CollectionViewCell
  35. cell.update(forGravatar: gravatars[indexPath.item])
  36. return cell
  37. }
  38. func collectionView(collectionView: UICollectionView, canFocusItemAtIndexPath indexPath: NSIndexPath) -> Bool {
  39. return true
  40. }
  41. }
  42. class CollectionViewCell : UICollectionViewCell {
  43. @IBOutlet var avatarView: UIImageView!
  44. override func prepareForReuse() {
  45. self.avatarView.image = nil
  46. }
  47. func update(forGravatar gravatar:Gravatar) {
  48. self.avatarView.setImageWithURL(gravatar.URL(size: self.bounds.size.width))
  49. }
  50. }