【发布时间】:2020-03-21 10:50:24
【问题描述】:
我正在构建一个 iOS 应用程序,其中有 4 种不同的 UICollectionViewn 设计 在 UITableView 中。
UICollectionViewCell 类名有:DealCollectionViewCell、FilterCollectionViewCell、ComboCollectionViewCell、BusinessCollectionViewCell
UITableViewCell 类名有:DealTableViewCell、BusinessTableViewCell、FilterTableViewCell、ComboTableViewCell
下面是UIViewController类代码(类名HomeViewControllerr)
类扩展
UIViewController, UITableViewDelegate, UITableViewDataSource, UICollectionViewDelegate, UICollectionViewDataSource
对于 UITableView
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 返回 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
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 时所做的)
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!
}
}
【问题讨论】:
-
你在上面的代码中遇到了什么问题?
-
@Hima 我在 dealUICollectionView 中有一个加载交易数组。如何在各自的 UICollectionView 中加载过滤器、组合和业务数组
-
您可以在此方法中添加相同的条件
if (collectionView == dealCollectionView)func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int并可以分别返回您的array.count。 -
@Hima 我做了它没有工作
-
@Hima 我不能将 collectionView == dealCollectionView 作为 collectionView 和 tableView 都在一个 viewController 中声明
标签: ios swift uitableview uicollectionview