UICollectionView内部的UICollectionView具有动态高度

问题描述

我正在尝试创建与App Store类似的布局。我有一个垂直的集合视图,在其中的每一行是另一个水平的集合视图。

我正在尝试使用动态高度,以便该单元格可以根据内部内容增加其高度。但是,这永远行不通,仅当我使用sizeforItemAt函数显式设置它时,它才起作用。我还希望能够控制宽度

我看过很多以前的问题,例如:UICollectionView,full width cells,allow autolayout dynamic height?,但是没有答案对我有用。

我对自己做错了真的很困惑。

import UIKit


class HomeViewController: UICollectionViewController {
    public override func viewDidLoad() {
        super.viewDidLoad()

        let collectionViewLayout = UICollectionViewFlowLayout()
        collectionViewLayout.scrollDirection = .vertical
        collectionViewLayout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize

        collectionView.collectionViewLayout = collectionViewLayout
        collectionView.translatesAutoresizingMaskIntoConstraints = false

        collectionView?.register(HorizontalCollectionView.self,forCellWithReuseIdentifier: HorizontalCollectionView.reuseIdentifier)
    }

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

    public override func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 4
    }

    public override func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: HorizontalCollectionView.reuseIdentifier,for: indexPath) as? HorizontalCollectionView {
            return cell
        }

        return UICollectionViewCell()
    }
}

Horizo​​ntalCollectionView.swift

import UIKit

class HorizontalCollectionView: UICollectionViewCell,UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout {

    static let reuseIdentifier = "HorizontalCollectionView"
    var cellSpancolumns: CGFloat?

    override init(frame: CGRect) {
        super.init(frame: frame)

        collectionView.register(UICollectionViewCell.self,forCellWithReuseIdentifier: "cell")

        collectionView.delegate = self
        collectionView.dataSource = self

        addSubview(collectionView)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented - Storyboards are not used.")
    }

    lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .horizontal
        layout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize

        let collectionView = UICollectionView(frame: bounds,collectionViewLayout: layout)
        collectionView.translatesAutoresizingMaskIntoConstraints = false
        collectionView.showsHorizontalScrollIndicator = false

        return collectionView
    }()

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

    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

    func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell",for: indexPath)
        if (indexPath as NSIndexPath).section == 0 {
            cell.backgroundColor = UIColor.blue
        } else {
            cell.backgroundColor = UIColor.red
        }

        return cell
    }
}

enter image description here

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)