gpt4 book ai didi

ios - 我试图在我的 ViewController 上放置多个 UICollectionView,但只有一个 UICollectionView 在模拟上运行

转载 作者:行者123 更新时间:2023-11-29 05:48:59 24 4
gpt4 key购买 nike

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

我尝试放置 print 语句来查看运行应用程序时是否使用了任何 if 语句。但是,打印的唯一语句也来自 allDealsCollectionView 的 else 语句。我认为这意味着问题源于创建 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 个显示为空的。

最佳答案

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

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

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

关于ios - 我试图在我的 ViewController 上放置多个 UICollectionView,但只有一个 UICollectionView 在模拟上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55860593/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com