如何获取 Collection 查看动态行数?

How to get Collection view Dynamic Row count?

我正在从 API

收集数据

样本API - 这有不同的省份和卫生区域

    {
        "summary": [
            {
                "cases": 520,
                "cumulative_cases": 63244,
                "cumulative_deaths": 614.0,
                "date": "11-04-2021",
                "deaths": 0.0,
                "health_region": "Calgary",
                "province": "Alberta"
            },
            {
                "cases": 139,
                "cumulative_cases": 12660,
                "cumulative_deaths": 123.0,
                "date": "11-04-2021",
                "deaths": 0.0,
                "health_region": "Central",
                "province": "Alberta"
            } 
   ]
}

**Collection查看代码**

//首先我要计算行数。我不确定我的做法是对是错

 func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
            
            var count: Int = 0
            for text in 1...100{
                if jsonData?.summary[text].province == dataFromSelection {
                    count = jsonData?.summary.count ?? 0
                    print("count block")
                }
            }
            return count
        }
        
        func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
            
            if collectionView == myCollectionView {
                    print("if block")
                if (dataFromSelection == jsonData?.summary[indexPath.row].province) {
                    print("im here")
                    let today = jsonData?.summary[indexPath.row].cases
                    let tDeaths = jsonData?.summary[indexPath.row].cumulative_deaths
                    let tCases = jsonData?.summary[indexPath.row].cumulative_cases
                    let regionName = jsonData?.summary[indexPath.row].health_region
                    
                    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "RegionCell", for: indexPath) as! RegionCell
                    cell.configure(region: regionName!, totCase: tCases!, todCases: today!, totDeaths: tDeaths!)
                    return cell
                }

Image of simulator

请帮我解决这个问题

首先为摘要详细信息创建数组,然后根据省份的选择过滤您的摘要数组。 例如:

让 summaryArray = [ [ “案例”:520, “cumulative_cases”:63244, “cumulative_deaths”:614.0, “日期”:“11-04-2021”, “死亡”:0.0, "health_region": "卡尔加里", “省”:“艾伯塔省” ], [ “案例”:139, “cumulative_cases”:12660, “cumulative_deaths”:123.0, “日期”:“11-04-2021”, “死亡”:0.0, "health_region": "中环", “省”:“艾伯塔省” ] ]

let filterArray = summaryArray.filter({($0["province"] as?String "") == dataFromSelection})

现在使用此 filterArray 计算行数并在集合视图单元格中显示详细信息