123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- //
- // PublishEditAddPicCell.swift
- // RainbowPlanet
- //
- // Created by Christopher on 2019/6/17.
- // Copyright © 2019 RainbowPlanet. All rights reserved.
- //
- import UIKit
- class PublishEditAddPicCell: UITableViewCell {
-
- private let maxImageCount: Int = 3
-
- typealias ChoosePicBlock = () -> Void
- var choosePicBlock : ChoosePicBlock?
-
- typealias DelPicTransBlock = (_ idxRow:Int?) -> Void
- var delPicTransBlock : DelPicTransBlock?
-
- var imgCount: Int = 0
- var goodsImageArr: Array<UIImage>? {
- didSet {
- imgCount = self.goodsImageArr?.count ?? 0
- }
- }
-
- class func cellWith(tableView:UITableView,indexPath:IndexPath) -> PublishEditAddPicCell {
- let ID = "PublishEditAddPicCell"
- tableView.register(PublishEditAddPicCell.self, forCellReuseIdentifier: ID)
- let cell : PublishEditAddPicCell = tableView.dequeueReusableCell(withIdentifier: ID, for: indexPath) as! PublishEditAddPicCell
- cell.indexPath = indexPath
- return cell
- }
-
- override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
- super.init(style: style, reuseIdentifier: reuseIdentifier)
- setupViews()
- setupLayouts()
- }
-
- required init?(coder aDecoder: NSCoder) {
- fatalError("init(coder:) has not been implemented")
- }
-
- var indexPath: IndexPath? {
- didSet {
-
- }
- }
-
- //MRAK: - 设置View
- private func setupViews() {
- self.selectionStyle = .none
-
- addSubview(collectionView)
- }
-
- private func setupLayouts() {
- collectionView.snp.remakeConstraints { (make) in
- make.top.equalTo(15)
- make.bottom.equalTo(-15)
- make.left.right.equalToSuperview()
- }
- }
-
- private lazy var collectionView: UICollectionView = {
- let collectionView = UICollectionView.init(frame: CGRect.zero, collectionViewLayout: collectionViewLayout)
- collectionView.backgroundColor = kffffffColor
- collectionView.delegate = self;
- collectionView.dataSource = self;
- collectionView.showsVerticalScrollIndicator = false
- collectionView.showsHorizontalScrollIndicator = false
- return collectionView
- }()
-
- private lazy var collectionViewLayout: UICollectionViewFlowLayout = {
- let collectionViewLayout = UICollectionViewFlowLayout.init()
- collectionViewLayout.minimumInteritemSpacing = 10
- collectionViewLayout.scrollDirection = .horizontal
- return collectionViewLayout
- }()
-
- }
- extension PublishEditAddPicCell: UICollectionViewDelegateFlowLayout,UICollectionViewDataSource,UICollectionViewDelegate {
- func numberOfSections(in collectionView: UICollectionView) -> Int {
- return 1
- }
-
- func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
- if imgCount < maxImageCount {
- return imgCount+1
- } else {
- return maxImageCount
- }
- }
-
- func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
- if imgCount < maxImageCount && indexPath.row == imgCount {
- // 添加图片
- let cell = PublishEditDefaultCollectionCell.cellWith(collectionView: collectionView, indexPath: indexPath)
- if imgCount == 0 {
- cell.noteStr = "添加图片"
- } else {
- cell.noteStr = "\(imgCount)/\(maxImageCount)"
- }
- return cell
- } else {
- // 展示图片
- let pCell = PublishEditAddImgCollectionCell.cellWith(collectionView: collectionView, indexPath: indexPath)
- if indexPath.row < imgCount {
- pCell.showImage = self.goodsImageArr![indexPath.row]
- }
- pCell.delPicBlock = {
- [weak self] (idxRow) in
- if let delPicTransBlock = self?.delPicTransBlock {
- delPicTransBlock(idxRow)
- }
- }
- return pCell
- }
- }
-
- func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
- return CGSize(width:100, height: 100)
- }
-
- func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
- return UIEdgeInsets(top: 0, left: 14, bottom: 0, right: 14)
- }
-
- func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
- if imgCount < maxImageCount && indexPath.row == imgCount {
- // 添加图片
- if let choosePicBlock = self.choosePicBlock {
- choosePicBlock()
- }
- }
- }
-
- }
|