ios - 我试图在我的 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 个显示为空的。
解决方案
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.
推荐阅读
- java - 寻找示例代码以从 aws 参数存储中读取参数值
- excel - 在excel中查找截止日期减去周末的剩余天数
- python - 当未传入数据框时,为什么我能够在函数中更改 Pandas 数据框的值?
- regex - 正则表达式:处理包含嵌套 JSON 对象的 CSV(逗号地狱!)
- pine-script - 我们如何才能使我们的策略只适用于一对特定的货币对?
- javascript - 未捕获的 DOMException:无法从 mapbox-gl-csp-worker.js 构造“Worker”
- google-cloud-platform - 如何恢复存储桶的权限
- sql - SQL:如何分组和求和?
- python - 石头剪刀布不打印结果
- javascript - 在 MongoDB 上一段时间后如何删除组件?