UITableViewCell 中的 UICollectionView 仅显示一个单元格
UICollectionView inside UITableViewCell shows only one cell
我有一个TableView
。
每个 UITableViewCell
是一个 UICollectionView
。
每个 UICollectionViewCell
只有一张图片。
出于某种原因,我的 UICollectionView
只显示一个单元格,尽管我的模型有更多单元格。
我在这里错过了什么?
分享我的代码:
产品VC
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)
}
}
类别单元格
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))
}
}
此外 - 有没有办法在我单击 UICollectionViewCell
时呈现(模态显示)不同的 UIViewController
?
您将要将闭合传递到表视图单元格,以便选择一个CollectionView单元格时,TableView Controller会知道它。 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
}
}
希望对您有所帮助。
我有一个TableView
。
每个 UITableViewCell
是一个 UICollectionView
。
每个 UICollectionViewCell
只有一张图片。
出于某种原因,我的 UICollectionView
只显示一个单元格,尽管我的模型有更多单元格。
我在这里错过了什么?
分享我的代码:
产品VC
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)
}
}
类别单元格
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))
}
}
此外 - 有没有办法在我单击 UICollectionViewCell
时呈现(模态显示)不同的 UIViewController
?
您将要将闭合传递到表视图单元格,以便选择一个CollectionView单元格时,TableView Controller会知道它。 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
}
}
希望对您有所帮助。