ZLCollectionView.swift 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 EZSwiftExtensions
  15. import CocoaLumberjack
  16. protocol ZLCollectionViewDelegate {
  17. func clickWithApp(_ app:O2App)
  18. }
  19. class ZLCollectionView: NSObject {
  20. fileprivate let itemNumberWithSection = 5
  21. var apps:[[O2App]] = [[],[]]
  22. var delegate:ZLCollectionViewDelegate?
  23. }
  24. extension ZLCollectionView:UICollectionViewDataSource{
  25. func numberOfSections(in collectionView: UICollectionView) -> Int {
  26. return 2
  27. }
  28. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  29. return apps[section].count
  30. }
  31. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  32. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "itemCell", for: indexPath) as! O2CollectionViewCell
  33. let app = self.apps[indexPath.section][indexPath.row]
  34. if app.storyBoard! == "webview" {
  35. cell.initImg(app: app)
  36. }else{
  37. cell.appIconImageView.image = UIImage(named: app.normalIcon!)
  38. cell.appIconImageView.highlightedImage = UIImage(named: app.selectedIcon!)
  39. }
  40. cell.appTitle.text = app.title
  41. return cell
  42. }
  43. }
  44. extension ZLCollectionView:UICollectionViewDelegateFlowLayout{
  45. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  46. return CGSize(width:SCREEN_WIDTH/CGFloat(itemNumberWithSection),height:80)
  47. }
  48. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
  49. return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
  50. }
  51. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
  52. return 0.0
  53. }
  54. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
  55. return 0.0
  56. }
  57. }
  58. extension ZLCollectionView:UICollectionViewDelegate{
  59. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  60. let app = self.apps[indexPath.section][indexPath.row]
  61. DDLogDebug("app \(app.title!) be clicked")
  62. self.delegate?.clickWithApp(app)
  63. }
  64. func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
  65. var reusableView:UICollectionReusableView = UICollectionReusableView(frame: .zero)
  66. if kind == UICollectionView.elementKindSectionHeader {
  67. reusableView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "OOAppMainheaderView", for: indexPath)
  68. let headerView = reusableView as! OOAppMainCollectionReusableView
  69. if indexPath.section == 0 {
  70. headerView.titleLabel.text = "主页应用"
  71. } else {
  72. headerView.titleLabel.text = "所有应用"
  73. }
  74. }else if kind == UICollectionView.elementKindSectionFooter {
  75. reusableView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "OOAppMainCollectionFooterView", for: indexPath)
  76. }
  77. return reusableView
  78. }
  79. }