我有一个TableView
。
每个UITableViewCell
是一个UICollectionView
。
每个UICollectionViewCell
只有一个图像。
由于某种原因,即使模型中有更多单元,我的UICollectionView
也只显示一个单元格。
我在这里想念什么?
共享我的代码:
ProductsVC
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)
}
}
CategoryCell
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
}
}
ProductCell
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
时,是否可以显示(模态显示)另一个UICollectionViewCell
?
您将希望将闭包传递给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
}
}
希望有所帮助。