NewMainCollectionView.swift 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. //
  2. // NewMainCollectionView.swift
  3. // O2Platform
  4. //
  5. // Created by 刘振兴 on 2017/3/12.
  6. // Copyright © 2017年 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. import EmptyDataSet_Swift
  10. protocol NewMainCollectionViewDelegate {
  11. func NewMainCollectionViewItemClickWithApp(_ app:O2App)
  12. func emptyTapClick()
  13. }
  14. class NewMainCollectionView: NSObject {
  15. var itemHeight:Double = 100.0
  16. var itemWidth:Double {
  17. return Double(SCREEN_WIDTH) / Double(apps.count)
  18. }
  19. //APP数据列表
  20. var apps:[O2App] = []
  21. var delegate:NewMainCollectionViewDelegate!
  22. override init() {
  23. super.init()
  24. }
  25. }
  26. extension NewMainCollectionView:UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{
  27. func numberOfSections(in collectionView: UICollectionView) -> Int {
  28. return apps.count > 0 ? 1 : 0
  29. }
  30. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  31. return apps.count
  32. }
  33. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  34. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "AppCollectionCell", for: indexPath) as! NewMainAppCollectionViewCell
  35. cell.setAppData(app: apps[indexPath.row])
  36. return cell
  37. }
  38. //FlowLayout
  39. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  40. return CGSize(width: itemWidth, height: itemHeight)
  41. }
  42. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
  43. return UIEdgeInsets(top: 1, left: 1, bottom: 1, right: 1)
  44. }
  45. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
  46. return CGSize(width: 0,height: 0)
  47. }
  48. func collectionView(_ collectionView:UICollectionView,layout collectionViewLayout:UICollectionViewLayout,referenceSizeForFooterInSection section: Int) -> CGSize {
  49. return CGSize(width: 0,height: 0)
  50. }
  51. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
  52. return 0
  53. }
  54. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
  55. return 0
  56. }
  57. //点击了其中一个
  58. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  59. //代理有设置执行代理
  60. if let d = delegate {
  61. d.NewMainCollectionViewItemClickWithApp(apps[indexPath.row])
  62. }
  63. }
  64. }
  65. extension NewMainCollectionView:EmptyDataSetSource,EmptyDataSetDelegate {
  66. func title(forEmptyDataSet scrollView: UIScrollView) -> NSAttributedString? {
  67. let attributes = [NSAttributedString.Key.font:UIFont(name: "PingFangSC-Regular", size: 16)!,NSAttributedString.Key.foregroundColor: UIColor(hex: "333333")]
  68. return NSAttributedString(string: "没有配置首页应用,请进入应用管理界面配置", attributes: attributes)
  69. }
  70. func buttonTitle(forEmptyDataSet scrollView: UIScrollView, for state: UIControl.State) -> NSAttributedString? {
  71. let attributes = [
  72. NSAttributedString.Key.font: UIFont(name: "PingFangSC-Regular", size: 15)!,
  73. NSAttributedString.Key.foregroundColor: UIColor.white,
  74. NSAttributedString.Key.backgroundColor: O2ThemeManager.color(for: "Base.base_color")!
  75. ]
  76. return NSAttributedString(string: "点击进入", attributes: attributes)
  77. }
  78. func emptyDataSet(_ scrollView: UIScrollView, didTapButton button: UIButton) {
  79. O2Logger.debug("emptyDataSet didTap Button")
  80. if let d = delegate {
  81. d.emptyTapClick()
  82. }
  83. }
  84. }