ios - 如何获取集合视图动态行数?
问题描述
我正在从 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"
}
]
}
**收藏查看代码**
//首先,我试图计算行数。我不确定我的方法是对还是错
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
}
请帮我解决这个问题
- 首先,我正在寻找要填充的行数,即 jsonData.summary[index] == Alberta(比如说)
- 第二个只有那些将阿尔伯塔省作为一个省出现的行让我知道我在这里做错了什么。我对 swift 还很陌生
解决方案
首先为摘要详细信息创建数组,然后根据选择的省份过滤您的摘要数组。前任:
让 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})
现在使用此filterArray来计算行数并在集合视图单元格中显示详细信息
推荐阅读
- php - 获取 Facebook 图片 URL
- tfs - Tfs 工作项搜索不返回包含
- javascript - 如何使用http模块将嵌套对象转换为节点中的查询字符串
- javascript - 反应状态给出未定义的新反应
- node.js - 使用 TypeScript 3 的 Node.js Express API 更新记录
- html - 单击 django 中的 href 链接时出现意外/不需要的 url 路径
- c++ - 将自定义 C++20 范围适配器与标准适配器相结合
- python - 如何使用小的单个图像并在整个窗口中重复它以使用 tkinter GUI 使其成为背景图像?
- javascript - 使用 Formik 和 material-ui stepper 上传文件
- r - 部分适用于用于检查一列中的列值是否存在于另一列中的语句