ViewController.swift 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // ViewController.swift
  2. // Copyright (c) 2011–2016 Alamofire Software Foundation ( http://alamofire.org/ )
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. import UIKit
  22. class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {
  23. @IBOutlet var collectionView: UICollectionView!
  24. var gravatars: [Gravatar] = []
  25. override func viewDidLoad() {
  26. super.viewDidLoad()
  27. for _ in 1...100 {
  28. let gravatar = Gravatar(
  29. emailAddress: UUID().uuidString,
  30. defaultImage: Gravatar.DefaultImage.Identicon,
  31. forceDefault: true
  32. )
  33. gravatars.append(gravatar)
  34. }
  35. }
  36. override func didReceiveMemoryWarning() {
  37. super.didReceiveMemoryWarning()
  38. // Dispose of any resources that can be recreated.
  39. }
  40. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  41. return gravatars.count
  42. }
  43. func numberOfSections(in collectionView: UICollectionView) -> Int {
  44. return 1
  45. }
  46. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  47. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewCell", for: indexPath) as! CollectionViewCell
  48. cell.update(forGravatar: gravatars[(indexPath as NSIndexPath).item])
  49. return cell
  50. }
  51. func collectionView(_ collectionView: UICollectionView, canFocusItemAt indexPath: IndexPath) -> Bool {
  52. return true
  53. }
  54. }
  55. class CollectionViewCell : UICollectionViewCell {
  56. @IBOutlet var avatarView: UIImageView!
  57. override func prepareForReuse() {
  58. self.avatarView.image = nil
  59. }
  60. func update(forGravatar gravatar:Gravatar) {
  61. self.avatarView.setImageWith(gravatar.URL(size: self.bounds.size.width) as URL)
  62. }
  63. }