【问题标题】:I am trying to place more than one UICollectionView on my ViewController but only one UICollectionView is running on the simulation我试图在我的 ViewController 上放置多个 UICollectionView,但模拟中只有一个 UICollectionView 正在运行
【发布时间】:2019-04-26 03:54:56
【问题描述】:

我正在尝试创建一个具有多个水平滚动视图的ViewController。为此,我决定使用UICollectionViews。但是,当我模拟我的应用程序时,唯一弹出的UICollectionViewallDealsCollectionView 下方代码中提供的else 语句中的那个。

我尝试放置打印语句以查看在运行应用程序时是否使用了任何 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 个。

【问题讨论】:

  • 您的所有collectionviews 的数据源是否设置正确?如果您覆盖它,您的numberOfItems(inSection:)numberOfSections(in:) 是什么样的?如果你中断这个函数,或者查看控制台打印,当应用程序运行时是否所有的 if/else 语句都会被命中?如果没有,并且您已经排除了 dataSource 和 numberOfXXX 功能,那么请检查您的插座并确保它们都正确连接,以便上述逻辑可以正常工作。此外,这不是问题的原因,但类应该大写,DessertsCollectionView 之类的变量不应该。
  • 是的,我的所有数据源都设置正确。但是,当应用程序运行时,if/else 语句不会被命中。此外,插座均已正确连接。这就是为什么我不确定问题可能是什么。另外,感谢您提供有关正确大小写的信息,我现在将对其进行更改。如果您对如何解决此问题有任何其他建议,我将不胜感激! @clarus
  • 你能显示collectionView(_ :, numberOfItemsInSection:)numberOfSections(in:) 的代码,如果你覆盖它吗?
  • 你在一个tableview中拥有所有这些collectionviews吗?
  • 有可能方法 - collectionView(_ :, numberOfItemsInSection:) 或 numberOfSections(in:) 为不同的 collectionViews 返回 0。或 indexPath 处的 SizeForItem 可能会为所有 collectionViews 返回 0。

标签: ios swift xcode uicollectionview uicollectionviewcell


【解决方案1】:

你需要正确检查当前collectionView是否等于当前类

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

对于 DessertsCollectionView 必须定义自定义类。

【讨论】:

  • 必须定义 DessertsCollectionView 自定义类是什么意思?
  • 对于您使用的每个集合,您需要使用自定义名称创建自己的类(如 DessertsCollectionView)并从 UICollectionView 继承。如果您使用情节提要,为每个 collectionView 定义您创建的类。 @Nebraska2Texas
  • 我已经为每个 CollectionViewCell 创建了类,并为单元格的情节提要中的每个单元格定义了类。这是你的意思吗?
  • @Nebraska2Texas 是的,你也需要为每个 collectionView 创建类
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多