1

i have a table view and i have placed a collection view inside the cell, i'm getting data from API and that data i'm passing to table view and collection view but when i run the app it crashes with this error,

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Lawon.KnowledgeVC collectionView:numberOfItemsInSection:]: unrecognized selector sent to instance 0x7fa90af0cbc0

My code for table view,

extension KnowledgeVC : UITableViewDelegate,UITableViewDataSource {

func numberOfSections(in tableView: UITableView) -> Int {
    return categoryArray.count
}

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


func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 35
}

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let headerView = Bundle.main.loadNibNamed("KnowledgeHeaderTVC", owner: self, options: nil)?.first as! KnowledgeHeaderTVC

    headerView.categoryNameLbl.text = categoryArray[section].catName
    headerView.articlesLbl.text = "\(categoryArray[section].blogArray.count)" + "articles"

    return headerView
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = knowledgeTableView.dequeueReusableCell(withIdentifier: "KnowledgeCell", for: indexPath) as! KnowledgeDetailTVC

    cell.categoryArray = categoryArray

    return cell
 }
}

This is my table view cell class where i have mad outlet for collection view and populating data in it and calling its delegate and datasource,

class KnowledgeDetailTVC: UITableViewCell,UICollectionViewDelegate, UICollectionViewDataSource {

@IBOutlet weak var categoryCollectionView : UICollectionView!
var categoryArray = [Category]()

override func awakeFromNib() {
    super.awakeFromNib()

    self.categoryCollectionView.reloadData()
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    print(categoryArray[section].blogArray.count)
    return categoryArray[section].blogArray.count
}

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

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "familyCell", for: indexPath) as! FamilyCVC

    cell.categoryLbl.text = categoryArray[indexPath.section].blogArray[indexPath.row].articleTitle
    let imageUrl = categoryArray[indexPath.section].blogArray[indexPath.row].imageUrl!
    print(imageUrl)
    cell.categoryImage.sd_setImage(with: URL(string: imageUrl), placeholderImage: UIImage(named: "person.jpeg"))
    //            cell.bgView.layer.masksToBounds = true
    //            cell.bgView.layer.shadowColor = UIColor.black.cgColor
    //            cell.bgView.layer.shadowOpacity = 3
    //            cell.bgView.layer.shadowOffset = CGSize(width: -1, height: 1)
    //            cell.bgView.layer.shadowRadius = 2
    //            cell.bgView.layer.shouldRasterize = true

    return cell
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

}

}

1 Answer 1

1

You may set the delegate & dataSource of the collectionView in the prototype cell to the KnowledgeVC in IB while implementation is inside the cell , but you should set them in awakeFromNib

self.categoryCollectionView.delegate = self
self.categoryCollectionView.dataSource = self
self.categoryCollectionView.reloadData()

Also it's better to refresh it inside cellForRowAt to avoid dequeuing problems

cell.categoryArray = categoryArray
cell.categoryCollectionView.reloadData()

There is no compile time error thrown in that case and that is a big problem of setting the delegate and dataSource in IB

Sign up to request clarification or add additional context in comments.

4 Comments

I have made IBOutlet in table view cell class for collection view and call its delegate and datasource in table view cell class as mention in code. @Sh_Khan
Do you still have the problem after following the answer ? Also don't you have a prototype table cell inside the vc that contains the collection ?
how to make IBOutlet of collection view in knowledge VC, will collection view is placed inside the cell of table view? Its showing me error. @Sh_Khan
outlet should be inside the cell anyway , you need to copy all delegate and dataSource methods of the collection to the vc and assign the delegate and dataSource of the collectionView to the vc inside cellForRowAt or leave it as it's now assigned when you posted the question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.