PublishEditAddPicCell.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. //
  2. // PublishEditAddPicCell.swift
  3. // RainbowPlanet
  4. //
  5. // Created by Christopher on 2019/6/17.
  6. // Copyright © 2019 RainbowPlanet. All rights reserved.
  7. //
  8. import UIKit
  9. class PublishEditAddPicCell: UITableViewCell {
  10. private let maxImageCount: Int = 3
  11. typealias ChoosePicBlock = () -> Void
  12. var choosePicBlock : ChoosePicBlock?
  13. typealias DelPicTransBlock = (_ idxRow:Int?) -> Void
  14. var delPicTransBlock : DelPicTransBlock?
  15. var imgCount: Int = 0
  16. var goodsImageArr: Array<UIImage>? {
  17. didSet {
  18. imgCount = self.goodsImageArr?.count ?? 0
  19. }
  20. }
  21. class func cellWith(tableView:UITableView,indexPath:IndexPath) -> PublishEditAddPicCell {
  22. let ID = "PublishEditAddPicCell"
  23. tableView.register(PublishEditAddPicCell.self, forCellReuseIdentifier: ID)
  24. let cell : PublishEditAddPicCell = tableView.dequeueReusableCell(withIdentifier: ID, for: indexPath) as! PublishEditAddPicCell
  25. cell.indexPath = indexPath
  26. return cell
  27. }
  28. override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
  29. super.init(style: style, reuseIdentifier: reuseIdentifier)
  30. setupViews()
  31. setupLayouts()
  32. }
  33. required init?(coder aDecoder: NSCoder) {
  34. fatalError("init(coder:) has not been implemented")
  35. }
  36. var indexPath: IndexPath? {
  37. didSet {
  38. }
  39. }
  40. //MRAK: - 设置View
  41. private func setupViews() {
  42. self.selectionStyle = .none
  43. addSubview(collectionView)
  44. }
  45. private func setupLayouts() {
  46. collectionView.snp.remakeConstraints { (make) in
  47. make.top.equalTo(15)
  48. make.bottom.equalTo(-15)
  49. make.left.right.equalToSuperview()
  50. }
  51. }
  52. private lazy var collectionView: UICollectionView = {
  53. let collectionView = UICollectionView.init(frame: CGRect.zero, collectionViewLayout: collectionViewLayout)
  54. collectionView.backgroundColor = kffffffColor
  55. collectionView.delegate = self;
  56. collectionView.dataSource = self;
  57. collectionView.showsVerticalScrollIndicator = false
  58. collectionView.showsHorizontalScrollIndicator = false
  59. return collectionView
  60. }()
  61. private lazy var collectionViewLayout: UICollectionViewFlowLayout = {
  62. let collectionViewLayout = UICollectionViewFlowLayout.init()
  63. collectionViewLayout.minimumInteritemSpacing = 10
  64. collectionViewLayout.scrollDirection = .horizontal
  65. return collectionViewLayout
  66. }()
  67. }
  68. extension PublishEditAddPicCell: UICollectionViewDelegateFlowLayout,UICollectionViewDataSource,UICollectionViewDelegate {
  69. func numberOfSections(in collectionView: UICollectionView) -> Int {
  70. return 1
  71. }
  72. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  73. if imgCount < maxImageCount {
  74. return imgCount+1
  75. } else {
  76. return maxImageCount
  77. }
  78. }
  79. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  80. if imgCount < maxImageCount && indexPath.row == imgCount {
  81. // 添加图片
  82. let cell = PublishEditDefaultCollectionCell.cellWith(collectionView: collectionView, indexPath: indexPath)
  83. if imgCount == 0 {
  84. cell.noteStr = "添加图片"
  85. } else {
  86. cell.noteStr = "\(imgCount)/\(maxImageCount)"
  87. }
  88. return cell
  89. } else {
  90. // 展示图片
  91. let pCell = PublishEditAddImgCollectionCell.cellWith(collectionView: collectionView, indexPath: indexPath)
  92. if indexPath.row < imgCount {
  93. pCell.showImage = self.goodsImageArr![indexPath.row]
  94. }
  95. pCell.delPicBlock = {
  96. [weak self] (idxRow) in
  97. if let delPicTransBlock = self?.delPicTransBlock {
  98. delPicTransBlock(idxRow)
  99. }
  100. }
  101. return pCell
  102. }
  103. }
  104. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  105. return CGSize(width:100, height: 100)
  106. }
  107. func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
  108. return UIEdgeInsets(top: 0, left: 14, bottom: 0, right: 14)
  109. }
  110. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  111. if imgCount < maxImageCount && indexPath.row == imgCount {
  112. // 添加图片
  113. if let choosePicBlock = self.choosePicBlock {
  114. choosePicBlock()
  115. }
  116. }
  117. }
  118. }