首页 > 解决方案 > 如果 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
    }

标签: iostablecell

解决方案


推荐阅读