UITableViewCell内的UICollectionView仅显示一个单元格

时间:2019-12-02 18:11:53

标签: swift uitableview uicollectionview uicollectionviewcell presentmodalviewcontroller

我有一个dev.mac.local

每个TableView是一个UITableViewCell

每个UICollectionView只有一张图片。

由于某种原因,即使我的模型有更多单元格,我的UICollectionViewCell也只显示一个单元格。

我在这里想念什么?

共享我的代码:

ProductsVC

UICollectionView

CategoryCell

import UIKit

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

    override func viewDidLoad()
    {
        super.viewDidLoad()

        categories.delegate = self
        categories.dataSource = self

        self.categories.separatorStyle = .none
        self.categories.register(UINib(nibName: "CategoryCell", bundle: nil), forCellReuseIdentifier: "categoryCell")
    }
}

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {

        let cell = categories.dequeueReusableCell(withIdentifier: "categoryCell", for: indexPath) as! CategoryCell

        cell.listOfProducts = Utils.inventory[indexPath.section].listOfProducts

        return cell
    }

    func numberOfSections(in tableView: UITableView) -> Int
    {
        return Utils.inventory.count
    }

    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView?
    {
        let headerText = UILabel()
        headerText.textColor = UIColor.systemPink
        headerText.adjustsFontSizeToFitWidth = true
        headerText.textAlignment = .right
        headerText.frame = CGRect(x: 20, y: 20, width: UIScreen.main.bounds.width - 40, height: 20)
        headerText.font = UIFont.boldSystemFont(ofSize: 25)

        headerText.text = Utils.inventory[section].name
        headerText.backgroundColor = .clear

        let headerView = UIView()
        headerView.addSubview(headerText)

        return headerView
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
    {
        let bounds = UIScreen.main.bounds
        let height = bounds.size.height

        return (height - (CGFloat(Utils.inventory.count) * 70))/CGFloat(Utils.inventory.count)
    }
}

ProductCell

import UIKit

class CategoryCell: UITableViewCell
{
    @IBOutlet private weak var products: UICollectionView!
    var listOfProducts: [Product] = []

    override func awakeFromNib()
    {
        super.awakeFromNib()

        self.products.delegate = self
        self.products.dataSource = self

        self.products.register(UINib(nibName: "ProductCell", bundle: nil), forCellWithReuseIdentifier: "productCell")
    }
    override func setSelected(_ selected: Bool, animated: Bool)
    {
        super.setSelected(selected, animated: animated)
    }
}

extension CategoryCell: UICollectionViewDelegate, UICollectionViewDataSource
{
    func numberOfSections(in collectionView: UICollectionView) -> Int
    {
        return 1
    }


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

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = products.dequeueReusableCell(withReuseIdentifier: "productCell", for: indexPath) as! ProductCell

        cell.productImage.image = listOfProducts[indexPath.row].image

        let height = UIScreen.main.bounds.height
        let frameHeight = ((height - (CGFloat(Utils.inventory.count) * 70))/CGFloat(Utils.inventory.count)) - 60

        cell.frame = CGRect(x: 20, y: 50, width: frameHeight, height: frameHeight)
        cell.productImage.frame = CGRect(x: 0, y: 0, width: frameHeight, height: frameHeight)

        Utils.makeControlRound(layer: cell.layer, borderWidth: 1, cornerRadius: 25, borderColor: cell.layer.backgroundColor)

        return cell
    }
}

还-当我点击import UIKit class ProductCell: UICollectionViewCell { @IBOutlet weak var productImage: UIImageView! override func awakeFromNib() { super.awakeFromNib() Utils.makeControlRound(layer: self.layer, borderWidth: 1, cornerRadius: 25, borderColor: #colorLiteral(red: 1, green: 1, blue: 1, alpha: 0)) Utils.makeControlRound(layer: self.productImage!.layer, borderWidth: 1, cornerRadius: 25, borderColor: #colorLiteral(red: 1, green: 1, blue: 1, alpha: 0)) } } 时,是否可以显示(模态显示)另一个UIViewController

1 个答案:

答案 0 :(得分:1)

您将需要将闭包传递给tableView单元,以便在选择collectionView单元之一时,tableView控制器将对此有所了解。 onProductSelected是您要在此处注意的地方。

class CategoryCell: UITableViewCell {
    var onProductSelected: ((Product) -> Void)?
    var listOfProducts: [Product] = []
}

extension CategoryCell: UICollectionViewDelegate, UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        onProductSelected?(listOfProducts[indexPath.item])
    }
}

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

    func presentViewController(for product: Product) {
        // Whatever
    }
}

extension ProductsVC: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = categories.dequeueReusableCell(withIdentifier: "categoryCell", for: indexPath) as! CategoryCell

        cell.onProductSelected = { [unowned self] product in
            self.presentViewController(for: product)
        }
        cell.listOfProducts = [] // Whatever

        return cell
    }
}

希望有帮助。