在视图控制器 Swift 中的 UITableView 中加载多个 UICollectionView [英] Load multiple UICollectionView in UITableView in view controller Swift
问题描述
我正在构建一个 iOS 应用程序,其中 UICollectionViewn 有 4 种不同的设计在 UITableView 中.
I am building an iOS application where there are 4 different designs of the UICollectionViewn in UITableView.
UICollectionViewCell 类名称为:DealCollectionViewCell、FilterCollectionViewCell、ComboCollectionViewCell、BusinessCollectionViewCell
UICollectionViewCell Class Name are : DealCollectionViewCell, FilterCollectionViewCell, ComboCollectionViewCell, BusinessCollectionViewCell
UITableViewCell 类名是:DealTableViewCell、BusinessTableViewCell、FilterTableViewCell、ComboTableViewCell
UITableViewCell Class Name are : DealTableViewCell, BusinessTableViewCell, FilterTableViewCell, ComboTableViewCell
下面是 UIViewController 类代码(类名 HomeViewControllerr)
Below is UIViewController class code (Class Name HomeViewControllerr)
类扩展
UIViewController, UITableViewDelegate, UITableViewDataSource, UICollectionViewDelegate, UICollectionViewDataSource
对于 UITableView
For UITableView
func tableView(_ tableView: UITableView, numberOfRowsInSection 部分: Int) -> Int {返回 4}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 4 }
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 0
{
let cell = tableView.dequeueReusableCell(withIdentifier: "DealTableViewCellId") as! DealTableViewCell
return cell
}
else if indexPath.row == 1
{
let cell = tableView.dequeueReusableCell(withIdentifier: "FilterTableViewCellId") as! FilterTableViewCell
return cell
}
else if indexPath.row == 2
{
let cell = tableView.dequeueReusableCell(withIdentifier: "ComboTableViewCellId") as! ComboTableViewCell
return cell
}
else
{
let cell = tableView.dequeueReusableCell(withIdentifier: "BusinessTableViewCellId") as! BusinessTableViewCell
return cell
}
}
对于 UICollectionView
For UICollectionView
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
return dealImageArray.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "DealCollectionViewCellId", for: indexPath) as? DealCollectionViewCell
return cell!
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let nextViewController = self.storyboard?.instantiateViewController(withIdentifier: "ProductViewControllerId") as! ProductViewController
self.present(nextViewController, animated:true, completion:nil)
//self.navigationController?.pushViewController(nextViewController, animated: true)
}
现在我想在 cellForItemAt 函数中添加多个 UICollectionViewCell.如下所示(这是我在不使用 UITableView 时所做的)
Now I want to add multiple UICollectionViewCell in cellForItemAt function. Like below (This I had done when I was not using UITableView)
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if (collectionView == dealCollectionView)
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "DealCollectionViewCellId", for: indexPath) as? DealCollectionViewCell
return cell!
}
else if (collectionView == comboCollectionView)
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ComboColllectionViewCellId", for: indexPath) as? ComboColllectionViewCell
return cell!
}
else
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "BusinessCollectionViewCell", for: indexPath) as? BusinessCollectionViewCell
return cell!
}
}
推荐答案
extension HomeViewController : UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 4
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "DealTableViewCell", for: indexPath) as! DealTableViewCell
cell.DealCollectionView.dataSource = self
cell.DealCollectionView.delegate = self
cell.DealCollectionView.tag = 10101
return cell
} else if indexPath.row == 1 {
let cell = tableView.dequeueReusableCell(withIdentifier: "BusinessTableViewCell", for: indexPath) as! BusinessTableViewCell
cell.BusinessCollectionView.dataSource = self
cell.BusinessCollectionView.delegate = self
cell.BusinessCollectionView.tag = 10102
return cell
} else if indexPath.row == 2 {
let cell = tableView.dequeueReusableCell(withIdentifier: "FilterTableViewCell", for: indexPath) as! FilterTableViewCell
cell.FilterCollectionView.dataSource = self
cell.FilterCollectionView.delegate = self
cell.FilterCollectionView.tag = 10103
return cell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: "ComboTableViewCell", for: indexPath) as! ComboTableViewCell
cell.ComboCollectionView.dataSource = self
cell.ComboCollectionView.delegate = self
cell.ComboCollectionView.tag = 10104
return cell
}
}
}
为每个tableview cell添加collection view并在tableview cell中设置delegate
Add the collection view to each tableview cell and set delegate in tableview cell
extension HomeViewController : UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if collectionView.tag == 10101 {
return 3
} else if collectionView.tag == 10102 {
return 3
} else if collectionView.tag == 10103 {
return 3
} else if collectionView.tag == 10104 {
return 3
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if collectionView.tag == 10101 {
let cell = collectionView.dequeueReusableCell(withIdentifier: "DealCollectionViewCell", for: indexPath) as! DealCollectionViewCell
return cell
} else if collectionView.tag == 10102 {
let cell = collectionView.dequeueReusableCell(withIdentifier: "BusinessCollectionViewCell", for: indexPath) as! BusinessCollectionViewCell
return cell
} else if collectionView.tag == 10103 {
let cell = collectionView.dequeueReusableCell(withIdentifier: "FilterCollectionViewCell", for: indexPath) as! FilterCollectionViewCell
return cell
} else if collectionView.tag == 10104 {
let cell = collectionView.dequeueReusableCell(withIdentifier: "ComboCollectionViewCell", for: indexPath) as! ComboCollectionViewCell
return cell
}
return UICollectionViewCell()
}
}
用于重新加载正在使用的 collectionView
For reload the collectionView in use
if let cell = self.tableView.cellForRow(at: IndexPath(row: i, section: 0)) as? tableViewCell {
cell.collectionView.reloadData()
}
这篇关于在视图控制器 Swift 中的 UITableView 中加载多个 UICollectionView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!