简体   繁体   English

如何获取集合视图动态行数?

[英]How to get Collection view Dynamic Row count?

I'm collecting data from an API我正在从 API 收集数据

sample API - This has different provinces & health regions示例 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 View Code ** **收藏查看代码**

//Firstly I'm trying to get a count for the rows. //首先,我试图计算行数。 I'm not sure my approach is right or wrong我不确定我的方法是对还是错

 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模拟器图像

Please help me out on this请帮我解决这个问题

  • First I'm looking for the count of rows to be populated ie jsonData.summary[index] == Alberta (say)首先,我正在寻找要填充的行数,即 jsonData.summary[index] == Alberta(比如说)
  • second only those rows to appear who have Alberta as a province let me know what I'm doing wrong here.第二个只有那些将阿尔伯塔省作为一个省出现的行让我知道我在这里做错了什么。 I'm fairly new to swift我对 swift 还很陌生

First create array for summary details and then Filter your summary array according to selection of province.首先为摘要详细信息创建数组,然后根据选择的省份过滤您的摘要数组。 ex:前任:

let summaryArray = [ [ "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" ] ]让 summaryArray = [ [“病例”:520,“累积病例”:63244,“累积死亡”:614.0,“日期”:“11-04-2021”,“死亡”:0.0,“健康区域”:“卡尔加里”,“省”:“阿尔伯塔”],[“病例”:139,“累积病例”:12660,“累积死亡”:123.0,“日期”:“11-04-2021”,“死亡”:0.0,“健康区域”:“中央”,“省”:“阿尔伯塔”]]

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

And now use this filterArray for the count of rows and display the details in collection view cell现在使用此filterArray来计算行数并在集合视图单元格中显示详细信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM