ZLCollectionView.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. //
  2. // ZLCollectionView.swift
  3. // O2Platform
  4. //
  5. // Created by 刘振兴 on 16/8/18.
  6. // Copyright © 2016年 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. import Alamofire
  10. import AlamofireImage
  11. import AlamofireObjectMapper
  12. import SwiftyJSON
  13. import ObjectMapper
  14. import CocoaLumberjack
  15. protocol ZLCollectionViewDelegate {
  16. func clickWithApp(_ app:O2App)
  17. }
  18. class ZLCollectionView: NSObject {
  19. fileprivate let itemNumberWithSection = 5
  20. var apps:[[O2App]] = [[],[]]
  21. var delegate:ZLCollectionViewDelegate?
  22. }
  23. extension ZLCollectionView:UICollectionViewDataSource{
  24. func numberOfSections(in collectionView: UICollectionView) -> Int {
  25. return 2
  26. }
  27. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  28. return apps[section].count
  29. }
  30. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  31. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "itemCell", for: indexPath) as! O2CollectionViewCell
  32. let app = self.apps[indexPath.section][indexPath.row]
  33. cell.setAppData(app: app)
  34. return cell
  35. }
  36. }
  37. extension ZLCollectionView:UICollectionViewDelegateFlowLayout{
  38. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  39. return CGSize(width:SCREEN_WIDTH/CGFloat(itemNumberWithSection),height:80)
  40. }
  41. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
  42. return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
  43. }
  44. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
  45. return 0.0
  46. }
  47. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
  48. return 0.0
  49. }
  50. }
  51. extension ZLCollectionView:UICollectionViewDelegate{
  52. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  53. let app = self.apps[indexPath.section][indexPath.row]
  54. DDLogDebug("app \(app.title!) be clicked")
  55. self.delegate?.clickWithApp(app)
  56. }
  57. func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
  58. var reusableView:UICollectionReusableView = UICollectionReusableView(frame: .zero)
  59. if kind == UICollectionView.elementKindSectionHeader {
  60. reusableView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "OOAppMainheaderView", for: indexPath)
  61. let headerView = reusableView as! OOAppMainCollectionReusableView
  62. if indexPath.section == 0 {
  63. headerView.titleLabel.text = "主页应用"
  64. } else {
  65. headerView.titleLabel.text = "所有应用"
  66. }
  67. }else if kind == UICollectionView.elementKindSectionFooter {
  68. reusableView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "OOAppMainCollectionFooterView", for: indexPath)
  69. }
  70. return reusableView
  71. }
  72. }