CollectionView单元格中适合内容的Collectionview

时间:2019-01-19 23:39:22

标签: swift uicollectionview

我有一个包含两个部分的collectionview,每个部分都有一个项目。该部分中的单元格包含collectionview,我需要将高度单元格调整为内容collectionview。

我的第一部分的高度固定为120,因为它的水平collectionview和高度未更改。但是对于第二部分,可以更改元素的数量,我需要显示所有项目(collectionview的scrollEnabled设置为false,scrolldirecton设置为vertical)。

我的问题是我需要显示一个具有两个滚动方向依赖部分的collectionview,并显示垂直collectionview的所有项目。

当我做collectionview.collectionViewLayout.collectionViewContentSize.height时,我的身高很好,但是我不知道如何将这个身高传递给父级viewcontroller。我搜索了一种动态解决方案来解决此问题。

class ViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

    // MARK: - UICollectionViewDataSource

    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return presenter.numberOfSections // == 2
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return presenter.numberOfItemsInSection // == 1
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueCell(InlineDisplayCollectionViewCell.self, for: indexPath)
        configureCell(cell, at: indexPath)

        return cell
    }

    // MARK: - UICollectionViewDelegateFlowLayout

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let type = presenter.typeAtIndexPath(indexPath)
        switch type {
        case .city:
            return CGSize(width: collectionView.frame.width, height: 120)
        case .suggestion:
            return CGSize(width: collectionView.frame.width, height: 820)
        default:
            return CGSize.zero
        }
    }


}


class InlineDisplayCollectionViewCell: UICollectionViewCell, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

    @IBOutlet weak var collectionView: UICollectionView!

    var itemType: ItemModelCollectionType! = .city {
        didSet {
            updateCell()
        }
    }
    var cities: [City]? {
        didSet {
            collectionView.reloadData()
        }
    }
    var suggestions: [Suggestion]? {
        didSet {
            collectionView.reloadData()
        }
    }

    override func awakeFromNib() {
        super.awakeFromNib()

        configureView()
    }

    // MARK: - UICollectionViewDataSource

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return itemType == .city ? cities?.count ?? 0 : suggestions?.count ?? 0
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell: UICollectionViewCell
        if itemType == .city {
            let cityCell = collectionView.dequeueCell(CityCollectionViewCell.self, for: indexPath)
            configure(cell: cityCell, at: indexPath)
            cell = cityCell
        } else {
            let suggestionCell = collectionView.dequeueCell(SquareCollectionViewCell.self, for: indexPath)
            configure(cell: suggestionCell, at: indexPath)
            cell = suggestionCell
        }
        return cell
    }

    // MARK: - UICollectionViewDelegateFlowLayout

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return itemType == .city ? 10 : 10
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return itemType == .city ? 0 : 10
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let size: CGSize
        if itemType == .city {
            size = CGSize(width: 100, height: 120)
        } else {
            let width = (collectionView.frame.width - (collectionView.contentInset.left + collectionView.contentInset.right) - 10) * 0.5
            let height = width
            size = CGSize(width: width, height: height)
        }

        return size
    }

    // MARK: - Private functions

    private func configureView() {
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.contentInset = UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20)
        collectionView.registerNib(CityCollectionViewCell.self, bundle: nil)
        collectionView.registerNib(SquareCollectionViewCell.self, bundle: nil)
    }

    private func updateCell() {
        if let flowLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
            flowLayout.scrollDirection = itemType == .city ? .horizontal : .vertical
        }
        collectionView.isScrollEnabled = itemType == .city
    }

    private func configure(cell: CityCollectionViewCell, at indexPath: IndexPath) {
        guard let city = cities?[indexPath.row] else { return }
        cell.configure(with: city)
    }

    private func configure(cell: SquareCollectionViewCell, at indexPath: IndexPath) {
        guard let suggestion = suggestions?[indexPath.row] else { return }
        cell.configure(with: suggestion)
    }

}

2 个答案:

答案 0 :(得分:0)

您不能那样做。使CollectionView单元出队并动态重用。最好的解决方法是在同一CollectionView中使用不同类型的单元格。

请检查有关此主题的问题:Get the sum of all collectionViewCells' heights inside a CollectionViewMultiple Cells for Comments and Reply in a CollectionView

答案 1 :(得分:0)

不幸的是,你不能做这样的事情。但是,您的简历中可以包含不同类型的单元格。您也可以在名为“ cellForItemAt”的函数中对其进行修改。