ZLCollectionView.swift 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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, section: Int)
  17. }
  18. class ZLCollectionView: NSObject {
  19. fileprivate let itemNumberWithSection = 5
  20. var apps:[[O2App]] = [[], [], []]
  21. var delegate:ZLCollectionViewDelegate?
  22. var isEdit = false
  23. }
  24. extension ZLCollectionView:UICollectionViewDataSource{
  25. func numberOfSections(in collectionView: UICollectionView) -> Int {
  26. return 3
  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. var icon = 0
  35. if isEdit {
  36. if indexPath.section == 0 {
  37. icon = 1
  38. } else {
  39. if isAdd2Main(app: app) {
  40. icon = 2
  41. } else {
  42. icon = 3
  43. }
  44. }
  45. } else {
  46. icon = 0
  47. }
  48. cell.setAppData(app: app, editIcon: icon)
  49. return cell
  50. }
  51. func isAdd2Main(app: O2App)-> Bool {
  52. let main = apps[0]
  53. if main.count > 0 {
  54. return main.contains { (a) -> Bool in
  55. return a.appId == app.appId
  56. }
  57. } else {
  58. return false
  59. }
  60. }
  61. }
  62. extension ZLCollectionView:UICollectionViewDelegateFlowLayout{
  63. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  64. return CGSize(width:SCREEN_WIDTH/CGFloat(itemNumberWithSection),height:80)
  65. }
  66. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
  67. return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
  68. }
  69. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
  70. return 0.0
  71. }
  72. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
  73. return 0.0
  74. }
  75. }
  76. extension ZLCollectionView:UICollectionViewDelegate{
  77. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  78. let app = self.apps[indexPath.section][indexPath.row]
  79. DDLogDebug("app \(app.title!) be clicked")
  80. self.delegate?.clickWithApp(app, section: indexPath.section)
  81. }
  82. func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
  83. var reusableView:UICollectionReusableView = UICollectionReusableView(frame: .zero)
  84. if kind == UICollectionView.elementKindSectionHeader {
  85. reusableView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "OOAppMainheaderView", for: indexPath)
  86. let headerView = reusableView as! OOAppMainCollectionReusableView
  87. if indexPath.section == 0 {
  88. headerView.titleLabel.text = L10n.applicationsMainApp
  89. } else if indexPath.section == 1 {
  90. headerView.titleLabel.text = L10n.applicationsNativeApp
  91. } else {
  92. headerView.titleLabel.text = L10n.applicationsPortalApp
  93. }
  94. }else if kind == UICollectionView.elementKindSectionFooter {
  95. reusableView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "OOAppMainCollectionFooterView", for: indexPath)
  96. }
  97. return reusableView
  98. }
  99. }