【问题标题】:Collectionview rows showing wrongly with JSON data in swiftCollectionview 行在 swift 中使用 JSON 数据错误显示
【发布时间】:2021-09-20 13:04:12
【问题描述】:

我在tableview单元格中使用collectionview

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

使用上面的代码,我得到 tableview 的行数是正确的,但是在 collectionview numberOfItemsInSection 出现错误.. 在每个部分中它只显示 collectionview 中的第一部分值.. 为什么?

请帮忙写代码

【问题讨论】:

    标签: ios swift uitableview uicollectionview


    【解决方案1】:

    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
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-08
    • 2019-02-22
    • 1970-01-01
    • 2019-11-19
    • 2021-10-13
    相关资源
    最近更新 更多