Collectionview rows showing wrongly with JSON data in swift

Viewed 79

I am using collectionview in tableview cell

 class CategoryNewVC: UIViewController {

@IBOutlet weak var categoryTableview: UITableView!
public var activeCategories : Array<Category_json>?

  override func viewDidLoad() {
    super.viewDidLoad()

    self.activeCategories = homeData?.result?.category_json?.filter({ (item) -> Bool in
        return item.status == "A"
    })
     categoryTableview.reloadData()
    }

this is the code for tableview and collectionview

extension CategoryNewVC : UITableViewDelegate,UITableViewDataSource{

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return self.activeCategories?.count ?? 0
}

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

    let cell = tableView.dequeueReusableCell(withIdentifier: "CategoryNewTableCell", for: indexPath) as! CategoryNewTableCell

            let indexData = self.activeCategories?[indexPath.row]
            cell.catNameLbl.text = indexData?.details?.first?.title 
            cell.activeCategories = self.activeCategories
            cell.clcSeller.reloadData()
            return cell
}
}

class CategoryNewTableCell: UITableViewCell,UICollectionViewDelegate,UICollectionViewDataSource{

@IBOutlet weak var catNameLbl: UILabel!
@IBOutlet weak var clcSeller: UICollectionView!

public var activeCategories : Array<Category_json>?

override func awakeFromNib() {
    super.awakeFromNib()
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return self.activeCategories?[section].sub_categories?.count ?? 0
}
    
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "SubCatCollectionCell", for: indexPath) as! SubCatCollectionCell

            let activesubCat = self.activeCategories?[indexPath.section].sub_categories
            let indexData = activesubCat?[indexPath.item]

            cell.lblTitle.text = langType == .en ? indexData?.details?.first?.title : indexData?.details?[1].title
    return cell
    
}
}

with the above code i am getting number of rows for tableview is correct, but in collectionview numberOfItemsInSection coming wrong.. here in every section it showing only first section values in collectionview.. why?

please do help with the code

1 Answers

In the CategoryNewTableCell, you need to make following changes -

public var subCategories : Array<..>? // Array of your subcategories for a particular tableViewCell

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return self.subCategories?.count ?? 0
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "SubCatCollectionCell", for: indexPath) as! SubCatCollectionCell

    // Change here
    let subCategory = self.subCategories?[indexPath.item]
    cell.lblTitle.text = langType == .en ? subCategory?.details?.first?.title : subCategory?.details?[1].title

    return cell
}

Finally you need to pass subCategories to your tableViewCell like this - from cellForRow implementation -

cell.subCategories = indexData?.sub_categories
Related