将 UICollectionViewCell 放入 UICollectionView

Put UICollectionViewCell into UICollectionView

我正在使用 LBTAComponents pod 这是一个更易于使用的 pod UICollectionView 无需注册任何东西并提供一个超级简单的锚定系统......在我两天前的第一个项目中我决定使用这个框架 但现在我有一个问题 uicollectionviewCells 我需要另一个 collectionview 我可以填充项目然后我需要它可以水平滚动

import LBTAComponents
import UIKit

class ProductCell: DatasourceCell {
    let cellId = "cellid"
    let collectionView : UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
        cv.backgroundColor = .black
        return cv
    }()

    override func setupViews() {
    super.setupViews()

        ProductCell.addSubview(collectionView)
        collectionView.frame = frame
        collectionView.register(UICollectionView.self, forCellWithReuseIdentifier: cellId)
        collectionView.dataSource = self

    }

}

extension ProductCell : UICollectionViewDataSource{

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 5
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath)
        cell.backgroundColor = .white
        return cell
    }

}

datasourceCell 等于此广告连播中的 UICollectionViewCell

现在我收到了这个错误:

instance member 'addsubview' cannot be used on type uiview did you use the value of this type instead?

你能帮帮我吗?

我尝试使用 self.addSubview(collectionView)

但是我又遇到了一个错误enter image description here

addsubview 是实例方法,不是 class 方法。所以你应该使用:

self.addSubview(collectionView)

而不是:

ProductCell.addSubview(collectionView)

您可以简单地将 UITableView 与包含 UICollectionView.

的自定义 UITableViewCells 一起使用

示例:

1.查看层次结构

2。 UIViewController 包含 UITableView

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate
{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return 2
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        return tableView.dequeueReusableCell(withIdentifier: "tcell", for: indexPath) as! TableCell
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
    {
        if indexPath.row == 0
        {
            return 120
        }
        else
        {
            return 150
        }
    }
}

3。自定义 UITableViewCell 包含 UICollectionView

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

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
    {
        return collectionView.dequeueReusableCell(withReuseIdentifier: "ccell", for: indexPath)
    }
}

4.输出截图