ZLCollectionView.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. if app.storyBoard! == "webview" {
  34. cell.initImg(app: app)
  35. }else{
  36. cell.appIconImageView.image = UIImage(named: app.normalIcon!)
  37. cell.appIconImageView.highlightedImage = UIImage(named: app.selectedIcon!)
  38. }
  39. cell.appTitle.text = app.title
  40. return cell
  41. }
  42. }
  43. extension ZLCollectionView:UICollectionViewDelegateFlowLayout{
  44. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  45. return CGSize(width:SCREEN_WIDTH/CGFloat(itemNumberWithSection),height:80)
  46. }
  47. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
  48. return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
  49. }
  50. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
  51. return 0.0
  52. }
  53. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
  54. return 0.0
  55. }
  56. }
  57. extension ZLCollectionView:UICollectionViewDelegate{
  58. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  59. let app = self.apps[indexPath.section][indexPath.row]
  60. DDLogDebug("app \(app.title!) be clicked")
  61. self.delegate?.clickWithApp(app)
  62. }
  63. func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
  64. var reusableView:UICollectionReusableView = UICollectionReusableView(frame: .zero)
  65. if kind == UICollectionView.elementKindSectionHeader {
  66. reusableView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "OOAppMainheaderView", for: indexPath)
  67. let headerView = reusableView as! OOAppMainCollectionReusableView
  68. if indexPath.section == 0 {
  69. headerView.titleLabel.text = "主页应用"
  70. } else {
  71. headerView.titleLabel.text = "所有应用"
  72. }
  73. }else if kind == UICollectionView.elementKindSectionFooter {
  74. reusableView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "OOAppMainCollectionFooterView", for: indexPath)
  75. }
  76. return reusableView
  77. }
  78. }