Varsha Vinod
Varsha Vinod

Reputation: 289

Assertion failure in UICollectionView inside UITableViewCell

My code has a UICollectionView inside a UITableViewCell. I have two different set of arrays named TopBrandArray & TopCategoryArray - which is being displayed in two different cells of a Table inside a collectionView.

The delegates and datasource has been connected to the HomeViewController in the main storyboard. Also, the name for the cells has been given.

There is no error while compiling the program but gets an expection while running the code.

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind: UICollectionElementKindCell with identifier TopCategoryCollectionID - must register a nib or a class for the identifier or connect a prototype cell in a storyboard

When I am trying to run by displaying only one single cell of the tableviewcell, the code works fine. But if am adding more cell the above mentioned error pops up. Here,I am adding the code:

@IBOutlet weak var homePageTable: UITableView!


override func viewDidLoad() {
    super.viewDidLoad()

    self.homePageTable.reloadData()
}


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 2
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    if indexPath.row == 0{
        let cell = tableView.dequeueReusableCell(withIdentifier: "TopCategoryID", for: indexPath) as! TopCategoriesTableViewCell
        cell.tablecollection1.reloadData()
        return cell
    }
    else{
        let cell = tableView.dequeueReusableCell(withIdentifier: "BrandTableID", for: indexPath) as! BrandsTableViewCell
        return cell
    }
}

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 162
}



func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    if section == 0{
        return topCategoryArray.count
    }
    else{
        return topBrandArray.count
    }

}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TopCategoryCollectionID", for: indexPath) as? TopCategoriesCollectionViewCell{
        cell.cat_image.image = UIImage(named: topCategoryArray[indexPath.row].image)
        print(topCategoryArray[indexPath.row].name)
        cell.cat_value.text = topCategoryArray[indexPath.row].name
    return cell
    }
    else{
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "BrandsCollectionID", for: indexPath) as! BrandsCollectionViewCell
        cell.layer.borderWidth = 1.5
        cell.layer.borderColor = UIColor.gray.cgColor
        cell.layer.cornerRadius = 4
        cell.brandimage.image = UIImage(named: topBrandArray[indexPath.row].image)
        return cell
    }
}
}

Since it is asking to register a nib or a class for the identifier, I do not know where inside the code should I write the code because when I wrote it in viewDidload(), an error popped up showing "Ambiguous reference to member collectionView(_:numberOfItemsInSection:)"

Upvotes: 0

Views: 215

Answers (1)

Mahendra
Mahendra

Reputation: 8924

The issue is with the registration of the cell with collection view. The collection view is unable to dequeue cell with the "TopCategoryCollectionID" identifier. You need to say to collection view that I'm going to use this cell as collection view cell.

You need to register TopCategoryCollectionID cell to collection view.

let nib = UINib(nibName: "TopCategoryCollectionID", bundle: nil)
collectionView.register(nib, forCellWithReuseIdentifier: "TopCategoryCollectionID")

If you have used cell in storyboard then no need to register cell again.

EDIT

As you have collection view inside your tableview cell so datasource and delegate of collection view must be implemented indie the respective cells.

You need to transfer the collection view code into table view cells.

Upvotes: 1

Related Questions