我收到了错误:-[UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:viewCategory:]中的断言失败

时间:2019-04-29 02:11:52

标签: ios swift xcode uicollectionview uicollectionviewcell

我有一个ViewController,上面有多个UICollectionViews。最初,我从两个(myFavoritesCollectionViewallDealsCollectionView)开始,该应用程序充分运行。但是,当我添加其他应用程序时,该应用程序开始崩溃。

我设置了一个异常断点并收到错误消息:

  

2019-04-28 21:00:44.802033-0500 DiscountDays_ [67279:13374922] ***-[UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:viewCategory:],/ BuildRoot / Library / Caches / com.apple中的声明失败.xbs / Sources / UIKitCore_Sim / UIKit-3698.103.12 / UICollectionView.m:5417

它突出显示了这一行:

let cell2 = collectionView.dequeueReusableCell(withReuseIdentifier: "hello", for: indexPath) as? FastFoodCollectionViewCell

下面是我的UIViewController上的扩展名:

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

        return allDiscounts.list.count
    }
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    if collectionView == 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
    cell1?.layer.cornerRadius = 11
        print("collectionviewlikes")
    return cell1!
    }
    else if collectionView == fastFoodCollectionView{
        let cell2 = collectionView.dequeueReusableCell(withReuseIdentifier: "hello", for: indexPath) as? FastFoodCollectionViewCell
        cell2?.fastCompanyLikedImage.image = UIImage(named: allDiscounts.list[fastFoodArray[indexPath.row]].imageName)
        cell2?.fastCompanyLikedName.text = allDiscounts.list[fastFoodArray[indexPath.row]].businessName
        cell2?.layer.cornerRadius = 11
        print("collectionviewfast")
        return cell2!

    }
    else if collectionView.isEqual(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
        cell3?.layer.cornerRadius = 11
        print("collectionviewdesserts")
        return cell3!
    }
    else if collectionView == 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
        cell4?.layer.cornerRadius = 11
        print("collectionviewsit")
        return cell4!
    }
    else if collectionView == 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
        cell5?.layer.cornerRadius = 11
        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
        cell6?.layer.cornerRadius = 11
        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)
    }
}
}

理想情况下,如果代码正常运行,将有一个UIViewController,其中有6个UICollectionViews,它们会水平滚动并显示不同的项目。

0 个答案:

没有答案