swift 中的 JSON 数据错误地显示 Collectionview 行

Collectionview rows showing wrongly with JSON data in swift

我在表格视图单元格中使用集合视图

 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()
    }

这是tableview和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
    
}
}

使用上面的代码,我得到的表视图的行数是正确的,但是在集合视图中 numberOfItemsInSection 出错了..在每个部分中,它仅显示集合视图中的第一部分值。 . 为什么?

请帮忙编写代码

CategoryNewTableCell中,您需要进行以下更改-

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
}

最后您需要像这样将 subCategories 传递给您的 tableViewCell - 来自 cellForRow 实现 -

cell.subCategories = indexData?.sub_categories