简体   繁体   中英

Put UICollectionViewCell into UICollectionView

I am using LBTAComponents pod this is a pod that makes easier to work with UICollectionView without any need to register anything and provides a super simple anchoring system ... and in the first project I've got two days ago I decided to use this framework but now I have a problem in one of uicollectionviewCells I need another collectionview that I can fill with items then I need it to be scrollable horizontally

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 is equal to UICollectionViewCell in this pod.

and now I am getting this ERROR :

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

could you please help me?

I tried to use self.addSubview(collectionView)

but I got another error enter image description here

addsubview is an instance method, not a class method. So you should use:

self.addSubview(collectionView)

instead of:

ProductCell.addSubview(collectionView)

You can simply use a UITableView with custom UITableViewCells containing a UICollectionView .

Example:

1. View Hierarchy

在此处输入图片说明

2. UIViewController containing 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. Custom UITableViewCell containing 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. Output Screenshot

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM