首页 > 解决方案 > 我试图在我的 ViewController 上放置多个 UICollectionView,但模拟中只有一个 UICollectionView 正在运行

问题描述

我正在尝试创建一个ViewController具有多个水平滚动视图。为此,我决定使用UICollectionViews. 但是,当我模拟我的应用程序时,唯一UICollectionView弹出的是allDealsCollectionView.

我尝试放置打印语句以查看在运行应用程序时是否使用了任何 if 语句。但是,唯一打印的语句也来自一个 else 语句allDealsCollectionView. 我认为这意味着问题源于创建 if 语句的某处。

下面是我的扩展中的一段代码ViewController

extension ViewController:UICollectionViewDelegate,UICollectionViewDataSource{
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    if collectionView == self.myFavoritesCollectionView{
        print(myEmptyArray.count)
       return myEmptyArray.count
    }
    else if collectionView == self.fastFoodCollectionView{
        return fastFoodArray.count
    }
    else if collectionView == self.DessertsCollectionView{
        return dessertsArray.count
    }
    else if collectionView == self.sitDownRestrauntsCollectionView{
        return sitDownArray.count
    }
    else if collectionView == self.otherThanFoodCollectionView{
        return otherArray.count
    }
    else {
        print(allDiscounts.list.count)

        return allDiscounts.list.count
    }
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    if collectionView == self.myFavoritesCollectionView {
    let cell1 = collectionView.dequeueReusableCell(withReuseIdentifier: "theLikes", for: indexPath) as? myLikesCollectionViewCell
    cell1?.companyLikedImage.image = UIImage(named: allDiscounts.list[myEmptyArray[indexPath.row]].imageName)
    cell1?.companyLikedName.text = allDiscounts.list[myEmptyArray[indexPath.row]].businessName
        print("collectionviewlikes")
    return cell1!
    }
    else if collectionView == self.fastFoodCollectionView {
        let cell2 = collectionView.dequeueReusableCell(withReuseIdentifier: "theFast", for: indexPath) as? FastFoodCollectionViewCell
        cell2?.fastCompanyLikedImage.image = UIImage(named: allDiscounts.list[fastFoodArray[indexPath.row]].imageName)
        cell2?.fastCompanyLikedName.text = allDiscounts.list[fastFoodArray[indexPath.row]].businessName
        print("collectionviewfast")
        return cell2!

    }
    else if collectionView == self.DessertsCollectionView {
        let cell3 =   collectionView.dequeueReusableCell(withReuseIdentifier: "theDesserts", for: indexPath) as? DessertsCollectionViewCell
        cell3?.dessertsCompanyLikedImage.image = UIImage(named: allDiscounts.list[dessertsArray[indexPath.row]].imageName)
        cell3?.dessertsCompanyLikedName.text = allDiscounts.list[dessertsArray[indexPath.row]].businessName
        print("collectionviewdesserts")
        return cell3!
    }
    else if collectionView == self.sitDownRestrauntsCollectionView {
        let cell4 = collectionView.dequeueReusableCell(withReuseIdentifier: "theSits", for: indexPath) as? sitDownCollectionViewCell
        cell4?.sitCompanyLikedImage.image = UIImage(named: allDiscounts.list[sitDownArray[indexPath.row]].imageName)
        cell4?.sitCompanyLikedName.text = allDiscounts.list[sitDownArray[indexPath.row]].businessName
        print("collectionviewsit")
        return cell4!
    }
    else if collectionView == self.otherThanFoodCollectionView {
        let cell5 = collectionView.dequeueReusableCell(withReuseIdentifier: "theOthers", for: indexPath) as? OtherCollectionViewCell
        cell5?.otherCompanyLikedImage.image = UIImage(named: allDiscounts.list[otherArray[indexPath.row]].imageName)
        cell5?.otherCompanyLikedName.text = allDiscounts.list[otherArray[indexPath.row]].businessName
        print("collectionviewother")
        return cell5!
    }
    else {
        let cell6 = collectionView.dequeueReusableCell(withReuseIdentifier: "allDeals", for: indexPath) as? allDealsCollectionViewCell
        cell6?.companyLikedImage1.image = UIImage(named: allDiscounts.list[indexPath.row].imageName)
        cell6?.companyLikedName1.text = allDiscounts.list[indexPath.row].businessName
        print("collectionviewall")
        return cell6!
    }
}


func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    if collectionView == self.myFavoritesCollectionView {
    // handle tap events
    ViewController.searchStruct.buttonTag = myEmptyArray[indexPath.row]
    performSegue(withIdentifier: "gotocompany1", sender: self)
    }
    else if collectionView == self.fastFoodCollectionView {
        ViewController.searchStruct.buttonTag = fastFoodArray[indexPath.row]
        performSegue(withIdentifier: "gotocompany1", sender: self)
    }
    else if collectionView == self.DessertsCollectionView {
        ViewController.searchStruct.buttonTag = dessertsArray[indexPath.row]
        performSegue(withIdentifier: "gotocompany1", sender: self)
    }
    else if collectionView == self.sitDownRestrauntsCollectionView {
        ViewController.searchStruct.buttonTag = sitDownArray[indexPath.row]
        performSegue(withIdentifier: "gotocompany1", sender: self)
    }
    else if collectionView == self.otherThanFoodCollectionView {
        ViewController.searchStruct.buttonTag = otherArray[indexPath.row]
        performSegue(withIdentifier: "gotocompany1", sender: self)
    }
    else {
        ViewController.searchStruct.buttonTag = indexPath.row
        performSegue(withIdentifier: "gotocompany1", sender: self)
    }
}

}

如果代码运行,应该有 6 个UICollectionViews都显示不同的图像和标签,而不是已经工作的和 6 个显示为空的。

标签: iosswiftxcodeuicollectionviewuicollectionviewcell

解决方案


You need to check correctly if current collectionView is equal current class

if collectionView.isEqual(DessertsCollectionView) {
   //current collectionView working
} 

For DessertsCollectionView custom class must be defined.


推荐阅读