设置嵌套在UITableViewCell中的UICollectionView的大小

我有一个UITableView

此表中的每个单元格都是一个称为xib的{​​{1}}。 此单元格由ProductCategoryCellUILabel组成。

UICollectionView中的每个单元格也是UICollectionView的{​​{1}},仅由xib组成。

我正在构建ProductCellUIImage,但看不到能够更改UITableViewUICollectionView的大小。

我得到的是UICollectionView,它可以像我希望的那样水平滚动,但是所有单元格都以两行显示。

我尝试遵循UICollectionViewCell,并添加了一些代码来修改UICollectionView的大小,但是我不能,我得到:“类型'ProductsCategoryCellVC'的扩展名不能从类UICollectionViewFlowLayout继承”

附加我的代码:

ProductsVC

UICollectionView

ProductCategoryCellVC

UICollectionViewFlowLayout

最后, ProductCellVC

import UIKit

class ProductsVC: UIViewController
{
    @IBOutlet weak var productsTableView: UITableView!

    var listOfProducts = [String: [Product]] ()
    var tableSize = CGSize (width: 0,height: 0)

    override func viewDidLoad()
    {
        super.viewDidLoad()
        self.productsTableView.separatorStyle = .none
        self.productsTableView.allowsSelection = false

        let screenSize = UIScreen.main.bounds
        let screenWidth = screenSize.width

        self.tableSize.width = screenWidth
        self.tableSize.height = screenWidth * 9 / 16

        self.productsTableView.rowHeight = self.tableSize.height

        for _ in listOfProducts.keys
        {
            self.productsTableView.register(UINib(nibName: "ProductsCategoryCell",bundle: nil),forCellReuseIdentifier: "productsCategoryCell")
        }

        self.productsTableView.delegate = self
        self.productsTableView.dataSource = self
    }
}

extension ProductsVC: UITableViewDelegate,UITableViewDataSource
{
    func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int
    {
        return listOfProducts.keys.count
    }

    func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let cell = self.productsTableView.dequeueReusableCell(withIdentifier: "productsCategoryCell") as! ProductsCategoryCellVC

        let relevantKey = Array(listOfProducts.keys)[indexPath.section]

        cell.productName.text = relevantKey
        cell.listOfProducts = listOfProducts[relevantKey]!

        cell.cellSize =  CGSize(width: self.tableSize.width * 0.4,height: self.tableSize.height / CGFloat(listOfProducts.keys.count) * 0.9)

        return cell
    }
}

我希望UICollectionView import UIKit class ProductsCategoryCellVC: UITableViewCell { @IBOutlet weak var productName: UILabel! @IBOutlet weak var productsCollectionView: UICollectionView! var cellSize = CGSize (width: 0,height: 0) { didSet { self.productsCollectionView.frame.size = cellSize } } var listOfProducts: [Product] = [] { didSet { for _ in listOfProducts { self.productsCollectionView.register(UINib(nibName: "ProductCell",forCellWithReuseIdentifier: "productCellVC") } } } override func awakeFromNib() { super.awakeFromNib() self.productsCollectionView.alwaysBounceHorizontal = true self.productsCollectionView.delegate = self self.productsCollectionView.dataSource = self } } extension ProductsCategoryCellVC: UICollectionViewDelegate,UICollectionViewDataSource { func collectionView(_ collectionView: UICollectionView,numberOfItemsInSection section: Int) -> Int { return listOfProducts.count } func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "productCellVC",for: indexPath) as! ProductCellVC cell.productImage.image = listOfProducts[indexPath.row].image return cell } } UICollectionView`小于运行该应用程序的设备宽度的一半。

我在做什么错了?

skymiandu02 回答:设置嵌套在UITableViewCell中的UICollectionView的大小

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3158574.html

大家都在问