Установка размера UICollectionView, который вложен в UITableViewCell - PullRequest
0 голосов
/ 05 ноября 2019

У меня есть UITableView.

Каждая ячейка в этой таблице - это xib, называемая ProductCategoryCell. Эта ячейка состоит из UILabel и UICollectionView.

Каждая ячейка в UICollectionView также является xib, которая называется ProductCell, и состоит только из UIImage.

Я строю UITableView и UICollectionView, но я не вижу возможности изменить размер ни UICollectionView, ни UICollectionViewCell.

Я получаю UICollectionView, который может, как мне хотелось бы, прокручивать горизонтально, но все ячейки представлены двумя линиями.

Я попытался соответствовать UICollectionViewFlowLayout и добавить некоторый код, чтобы изменить размерUICollectionView, но я не могу, я получаю: "Расширение типа 'ProductsCategoryCellVC' не может наследовать от класса UICollectionViewFlowLayout"

Прикрепление моего кода:

ProductsVC

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
    }
}

ProductCategoryCellVC

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", bundle: nil), 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
    }
}

И, наконец, ProductCellVC

import UIKit

class ProductCellVC: UICollectionViewCell
{
    @IBOutlet weak var productImage: UIImageView!

    override func awakeFromNib()
    {
        super.awakeFromNib()

        self.layer.borderWidth = 1
        self.layer.cornerRadius = 25
    }
}

Я хочу, чтобы 'UICollectionView to have one row of products, and the width of each cell (product) in the UICollectionView` был немного меньше половины ширины устройства, на котором запущено приложение.

Что я делаю не так?

...