ios - 如果 id 相同,则将项目分组在同一对象下
问题描述
在我的应用程序中,我得到的响应方式可以说我在标题“STRUCTURE”下选择了两个问题,所以基本上当我们想在表格单元格中显示该问题并在标题视图中显示标题“STRUCTURE”时,然后当我们点击标题时,它会展开并显示单元格,即“结构”下的问题 1 和问题 2,但在我的情况下,我得到如下响应:
"questions": [
{
"id": 175,
"question": {
"id": 29,
"question": "Check apron and guards are intact and secure.",
"job_option": {
"id": 34,
"name": "Structure"
}
},
"comment": "",
"image": null,
"answer": 1,
"answer_by": 7
},
{
"id": 176,
"question": {
"id": 27,
"question": "Check for cracks, bends, tents or broken parts",
"job_option": {
"id": 34,
"name": "Structure"
}
},
"comment": "",
"image": null,
"answer": 0,
"answer_by": null
},
{
"id": 190,
"question": {
"id": 24,
"question": "Check tyres and rims for wear and damage",
"job_option": {
"id": 44,
"name": "Tyres"
}
},
"comment": "",
"image": null,
"answer": 0,
"answer_by": null
}
],
正如在 joboptions 对象下的响应中,有一个带有 id 和 name 的结构,就在下面还有另一个对象,它带有带有 name 和 id 的作业选项“STRUCTURE”,并且在这两种情况下 id 都是相同的,因为这是我在选择时添加的两个问题在标题“STRUCTURE”和屏幕截图中,我得到了两个标题作为结构和我不想要的个别问题,我希望必须有一个标题和这两个问题。我如何在不改变我的模型结构的情况下实现它
主类文件:
func numberOfSections(in tableView: UITableView) -> Int {
return self.jobQuestionsList.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if let questions = self.jobQuestionsList[section].question?.question, questions.count > 0 {
return self.selectedHeaderIndex.contains(section) ? 0 : 1
} else {
return 0
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell:QuestionsDataCell = tableView.dequeueCell(withType: QuestionsDataCell.self) else { return EmptyTableCell }
let obj = self.jobQuestionsList[indexPath.section]
cell.questionLbl.text = obj.question?.question
cell.superVC = self
return cell
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let header = tableView.dequeueReusableHeaderFooterView(withIdentifier: "QuestionsHeaderView") as! QuestionsHeaderView
header.queHeaderLbl.text = self.jobQuestionsList[section].question?.jobOption?.name
header.animateArrow(isCollapsed: !self.selectedHeaderIndex.contains(section))
header.delegate = self
header.headerExpandBtn.tag = section
header.tintColor = .clear
return header
}
解决方案
推荐阅读
- log4j - log4j 是否支持符号链接日志文件?
- java - 嵌入式 tomcat java 应用程序正在运行,但无法访问服务器
- java - org.postgresql.util.PSQLException:致命:剩余的连接槽保留用于非复制超级用户连接
- three.js - 如何在threejs中保持精灵垂直堆叠?
- git - 一次运行删除多个分支
- android - 无法执行 aapt :app:processDebugResources FAILED
- javascript - 通过扩展点击页面上的一个元素,并在弹出窗口中获取点击的元素数量
- shell - jq显示一个json文件的不同字段
- class - 以不同的单位重用对象的属性
- sql - 在 PeopleSoft 查询管理器中按会计年度确定折旧