123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283 |
- //
- // OrderCommentTableViewCell.swift
- // RainbowPlanet
- //
- // Created by Christopher on 2019/5/17.
- // Copyright © 2019 RainbowPlanet. All rights reserved.
- //
- import UIKit
- import RxSwift
- import IQKeyboardManagerSwift
- class OrderCommentTableViewCell: UITableViewCell {
-
- let disposeBag = DisposeBag()
-
- private let maxImageCount: Int = 6
-
- typealias CommentTextViewClosure = (_ text: String) -> Void
- var commentTextViewClosure : CommentTextViewClosure?
-
- typealias StartChangedBlock = (_ starValue:Int?) -> Void
- var startChangedBlock : StartChangedBlock?
-
- 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
- }
- }
-
- var orderModelDetailModel: OrderModelDetailModel? {
- didSet {
- // 商品图片
- iconImageView.kf.setImage(with: kURLImage(name: orderModelDetailModel?.productImg ?? "pic_preload"), placeholder: kImage(name: "pic_preload"))
- starView.value = CGFloat(self.orderModelDetailModel?.starValue ?? 5)
- }
- }
-
-
- class func cellWith(tableView:UITableView,indexPath:IndexPath) -> OrderCommentTableViewCell {
- let ID = "OrderCommentTableViewCell"
- tableView.register(OrderCommentTableViewCell.self, forCellReuseIdentifier: ID)
- let cell : OrderCommentTableViewCell = tableView.dequeueReusableCell(withIdentifier: ID, for: indexPath) as! OrderCommentTableViewCell
- 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(cmtBackView)
- cmtBackView.addSubview(iconImageView)
- cmtBackView.addSubview(titleLabel)
- cmtBackView.addSubview(starView)
-
- addSubview(cmtTextView)
- addSubview(collectionView)
- }
-
- private func setupLayouts() {
- cmtBackView.snp.makeConstraints { (make) in
- make.top.left.right.equalToSuperview()
- make.height.equalTo(91)
- }
- iconImageView.snp.makeConstraints { (make) in
- make.top.equalToSuperview().offset(16)
- make.left.equalToSuperview().offset(14)
- make.size.equalTo(55)
- }
- titleLabel.snp.makeConstraints { (make) in
- make.left.equalTo(iconImageView.snp_right).offset(10)
- make.centerY.equalTo(iconImageView)
- // make.width.equalTo(56)
- make.height.equalTo(20)
- }
- starView.snp.makeConstraints { (make) in
- make.left.equalTo(titleLabel.snp_right).offset(12)
- make.centerY.equalTo(iconImageView)
- make.width.equalTo(122)
- make.height.equalTo(20)
- }
-
- cmtTextView.snp.makeConstraints { (make) in
- make.top.equalTo(cmtBackView.snp_bottom)
- make.left.equalTo(14)
- make.right.equalTo(-14)
- make.height.equalTo(100)
- }
-
- collectionView.snp.makeConstraints { (make) in
- make.top.equalTo(cmtTextView.snp_bottom)
- make.left.right.equalToSuperview()
- make.height.equalTo(200)
- make.bottom.equalToSuperview()
- }
-
- }
-
- private lazy var cmtBackView: UIView = {
- let cmtBackView = UIView()
- return cmtBackView
- }()
-
- private lazy var iconImageView: UIImageView = {
- let iconImageView = UIImageView()
- iconImageView.contentMode = .scaleAspectFit
- iconImageView.contentMode = .scaleAspectFill
- iconImageView.masksToBounds = true
- return iconImageView
- }()
-
- private lazy var titleLabel: UILabel = {
- let titleLabel = UILabel()
- titleLabel.text = "商品评分"
- titleLabel.textColor = k333333Color
- titleLabel.font = kBoldFont14
- titleLabel.textAlignment = .left
- titleLabel.numberOfLines = 1
- return titleLabel
- }()
-
- private lazy var starView: SwiftyStarRatingView = {
- let starView = SwiftyStarRatingView()
- starView.emptyStarImage = kImage(name: "star_rating_empty")
- starView.halfStarImage = kImage(name: "star_rating_half")
- starView.filledStarImage = kImage(name: "star_rating_filled")
- starView.allowsHalfStars = false
- starView.addTarget(self, action: #selector(starViewDidClick), for: .valueChanged)
- return starView
- }()
-
- private lazy var cmtTextView: IQTextView = {
- let cmtTextView = IQTextView()
- cmtTextView.backgroundColor = kfafafaColor
- cmtTextView.textColor = k333333Color
- cmtTextView.font = kRegularFont14
- cmtTextView.placeholder = "输入商品评价..."
- cmtTextView.placeholderTextColor = k999999Color
- cmtTextView.delegate = self
- return cmtTextView
- }()
-
- 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.minimumLineSpacing = 10
- collectionViewLayout.minimumInteritemSpacing = 0
- return collectionViewLayout
- }()
-
- //加载数据
- func reloadData() {
- //collectionView重新加载数据
- self.collectionView.reloadData()
- //更新collectionView的高度约束
- let contentSize = self.collectionView.collectionViewLayout.collectionViewContentSize
- collectionView.snp.remakeConstraints { (make) in
- make.top.equalTo(cmtTextView.snp_bottom)
- make.left.right.equalToSuperview()
- make.height.equalTo(contentSize.height)
- make.bottom.equalToSuperview()
- }
- self.collectionView.collectionViewLayout.invalidateLayout()
- }
-
- // 评分视图点击
- @objc func starViewDidClick(starView: SwiftyStarRatingView) {
- let starValue = Int(starView.value)
- if let startChangedBlock = self.startChangedBlock {
- startChangedBlock(starValue)
- }
- }
-
- }
- extension OrderCommentTableViewCell: 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 aCell = OrderCommentDefaultCollectionCell.cellWith(collectionView: collectionView, indexPath: indexPath)
- if imgCount == 0 {
- aCell.noteStr = "添加照片"
- } else {
- aCell.noteStr = "\(imgCount)/\(maxImageCount)"
- }
- return aCell
- } else {
- // 展示图片
- let pCell = OrderCommentAddImgCollectionCell.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:(kScreenWidth-30-5*3)/4, height: (kScreenWidth-30-5*3)/4)
- }
-
- func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
- return UIEdgeInsets(top: 10, left: 15, bottom: 0, right: 15)
- }
-
- func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
- if imgCount < maxImageCount && indexPath.row == imgCount {
- if let choosePicBlock = self.choosePicBlock {
- choosePicBlock()
- }
- }
- }
-
- }
- extension OrderCommentTableViewCell: UITextViewDelegate {
-
- func textViewDidChange(_ textView: UITextView) {
- if textView == cmtTextView {
- var fullStr = textView.text ?? ""
- if textView.text?.count ?? 0 > 180 {
- fullStr = String(fullStr.prefix(150)) as String
- textView.text = fullStr
- }
-
- if let commentTextViewClosure = self.commentTextViewClosure {
- commentTextViewClosure(self.cmtTextView.text ?? "")
- }
- }
- }
-
- }
|