// // SBTContentViewCell.swift // SolarBT // // Created by weclouds on 2019/3/11. // Copyright © 2019 weclouds. All rights reserved. // import UIKit class SBTContentViewCell: UITableViewCell { var cellCollectionView:UICollectionView? //回调方法 var cellForItemAtIndexPathBlock: ((IndexPath)->String)? var numberOfItemsInSectionBlock: ((Int)->Int)? var sizeForItemAtIndexPathBlock: ((UICollectionViewLayout?,IndexPath)->CGSize)? var ContentViewCellDidScrollBlock: ((UIScrollView)->Void)? var cellWithColorAtIndexPathBlock: ((IndexPath)->Bool)? var didSelectItemBlock: ((IndexPath)->Void)? override func awakeFromNib() { super.awakeFromNib() // Initialization code } override func setSelected(_ selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) } override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { super.init(style: style, reuseIdentifier: reuseIdentifier) let layout = UICollectionViewFlowLayout() layout.minimumLineSpacing = 1.0 //间隔 layout.minimumInteritemSpacing = 0; layout.scrollDirection = .horizontal //滚动方向 cellCollectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height), collectionViewLayout: layout) cellCollectionView?.showsHorizontalScrollIndicator = false if #available(iOS 11.0, *) { cellCollectionView?.contentInsetAdjustmentBehavior = .always } //设置边框 cellCollectionView?.backgroundColor = UIColor.red cellCollectionView?.layer.backgroundColor = UIColor.lightGray.cgColor cellCollectionView?.layer.borderWidth = 1 cellCollectionView?.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "inner.cell") cellCollectionView?.delegate = self cellCollectionView?.dataSource = self contentView.addSubview(cellCollectionView!) } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } } extension SBTContentViewCell : UICollectionViewDelegate,UICollectionViewDataSource{ //滚动代理 func scrollViewDidScroll(_ scrollView: UIScrollView) { if let contentViewCellDidScrollBlock = ContentViewCellDidScrollBlock { contentViewCellDidScrollBlock(scrollView) } } func numberOfSections(in collectionView: UICollectionView) -> Int { return 1 } func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return numberOfItemsInSectionBlock!(section) } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { return sizeForItemAtIndexPathBlock!(collectionViewLayout,indexPath) } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { var intercell : UICollectionViewCell? = collectionView.dequeueReusableCell(withReuseIdentifier: "inner.cell", for: indexPath) if intercell != nil{ for view in intercell!.contentView.subviews{ view.removeFromSuperview() } } intercell!.layer.borderColor = UIColor.lightGray.cgColor intercell!.layer.borderWidth = 1.0 let hasColor = false if hasColor == cellWithColorAtIndexPathBlock!(indexPath){ intercell!.backgroundColor = hasColor ? UIColor.lightGray : UIColor.white let cellsize : CGSize = sizeForItemAtIndexPathBlock!(nil,indexPath) let width = cellsize.width let height = cellsize.height let rect = CGRect(x: 0, y: 0, width: width, height: height) let label = UILabel(frame: rect) label.text = cellForItemAtIndexPathBlock!(indexPath) label.textColor = UIColor.black label.textAlignment = .center intercell?.contentView.addSubview(label) } return intercell! } func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { if let cellDidSelectBlock = didSelectItemBlock { cellDidSelectBlock(indexPath) } } }