在自定义FlowLayout无法正常工作的TableViewCell中自动调整CollectionView

时间:2018-06-06 18:56:42

标签: ios swift uitableview uicollectionview cell

我在tableViewCell中有一个CollectionView。此collectionView具有自动调整大小的单元格,其自定义FlowLayout然后向左对齐。但是collectionView没有正确调整大小,只在滚动时调整大小,但很多都是错误的。

这是我的tableView委托:

extension SearchStationsViewController: UITableViewDataSource, UITableViewDelegate {

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
   let cell = tableView.dequeueReusableCell(withIdentifier: "SearchStationsCell") as! SearchStationsTableViewCell
    cell.delegate = self
    cell.configure(station: (self.stationList?.stations![indexPath.row])!)
    return cell
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return stationCount
}

}

这是我的tableView Cell配置

class SearchStationsTableViewCell: UITableViewCell {

var delegate : SearchStationsTableViewCellProtocol?
static let heightForRow = 234
var numberOfRowsInCollection = 0
var station : Station?
let alignedFlowLayout = FlowLayout(
    minimumInteritemSpacing: 3,
    minimumLineSpacing: 3,
    sectionInset: UIEdgeInsets(top: 3, left: 3, bottom: 3, right: 3)
)

override func awakeFromNib() {
    super.awakeFromNib()
    self.background.shadowWith(color: UIColor.black.cgColor, opacity: 0.1, size: CGSize(width: 0.0, height: 3.0), radius: 5.0)
    registerCollectionViewCell()
    collectionView.delegate = self
    collectionView.dataSource = self
    collectionView.isScrollEnabled = false
    collectionView.collectionViewLayout = alignedFlowLayout
    alignedFlowLayout.estimatedItemSize = UICollectionViewFlowLayoutAutomaticSize
}

func registerCollectionViewCell() {
    collectionView.register(UINib(nibName: "StationFiltersCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "StationFiltersCollectionViewCell")
}



func configure(station: Station){
    self.numberOfRowsInCollection = (station.servicos?.count)!
    self.servicos = station.servicos!
    self.collectionView.reloadData()
}
}

  extension SearchStationsTableViewCell: 
  UICollectionViewDataSource,UICollectionViewDelegate, 
  UICollectionViewDelegateFlowLayout{


func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{
    return numberOfRowsInCollection
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "StationFiltersCollectionViewCell", for: indexPath) as! StationFiltersCollectionViewCell
    //CollectionView Cell Configuration
    return cell
}

}

这是我的自定义FlowLayout

class FlowLayout: UICollectionViewFlowLayout {

required init(minimumInteritemSpacing: CGFloat = 0, minimumLineSpacing: CGFloat = 0, sectionInset: UIEdgeInsets = .zero) {
    super.init()

    estimatedItemSize = UICollectionViewFlowLayoutAutomaticSize
    self.minimumInteritemSpacing = minimumInteritemSpacing
    self.minimumLineSpacing = minimumLineSpacing
    self.sectionInset = sectionInset
   // sectionInsetReference = .fromSafeArea
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
    let layoutAttributes = super.layoutAttributesForElements(in: rect)!.map { $0.copy() as! UICollectionViewLayoutAttributes }
    guard scrollDirection == .vertical else { return layoutAttributes 
 }

    // Filter attributes to compute only cell attributes
    let cellAttributes = layoutAttributes.filter({ $0.representedElementCategory == .cell })

    // Group cell attributes by row (cells with same vertical center) and loop on those groups
    for (_, attributes) in Dictionary(grouping: cellAttributes, by: { ($0.center.y / 10).rounded(.up) * 10 }) {
        // Set the initial left inset
        var leftInset = sectionInset.left

        // Loop on cells to adjust each cell's origin and prepare leftInset for the next cell
        for attribute in attributes {
            attribute.frame.origin.x = leftInset
            leftInset = attribute.frame.maxX + minimumInteritemSpacing
        }
    }

    return layoutAttributes
}

}

这是我的CollectionView Cell(collectionView中的单元格,在tableViewCell中)

class StationFiltersCollectionViewCell: UICollectionViewCell {

let heightForRow = 30
@IBOutlet weak var filterName : UILabel?
@IBOutlet weak var filterImage : UIImageView?
@IBOutlet weak var borderView : UIView?
@IBOutlet weak var widthConstraint: NSLayoutConstraint!


override func awakeFromNib() {
    super.awakeFromNib()
    borderView?.layer.cornerRadius = 5
    borderView?.layer.borderWidth = 1
    borderView?.layer.borderColor = UIColor(red:0.59, green:0.59, blue:0.59, alpha:1.0).cgColor
   // self.contentView.translatesAutoresizingMaskIntoConstraints = false
    widthConstraint.constant = CGFloat(self.heightForRow)
}

}

所以结果是这样的:

滚动前

Before Scrolling

滚动后

After Scrolling

有人可以帮助我吗?

0 个答案:

没有答案