迅速在tableview中嵌套部分

时间:2018-11-22 08:34:55

标签: ios json swift mvvm uitableviewsectionheader

{
    "data":[
            {

            "mainquestion":"was the staff",
            "items":[

              {
              "button_type":"2",
              "question": "Gender",
              "options": ["Male","Female"]

              },
              {
              "button_type":"2",
              "question": "How old are you",
              "options": ["Under 18","Age 18 to 35","Age 36 to 50","Age 51 to 70","70 and above"]

             }
           ]
            }, {

            "mainquestion":"Please tell the feedback",
            "items":[

                     {
                     "button_type":"2",
                     "question": "Gender",
                     "options": ["Male","Female"]

                     },
                     {
                     "button_type":"2",
                     "question": "How old are you",
                     "options": ["Under 18","Age 18 to 35","Age 36 to 50","Age 51 to 70","70 and above"]

                     },
                     {
                     "button_type":"2",
                     "question": "How old are you",
                     "options": ["Under 18","Age 18 to 35","Age 36 to 50","Age 51 to 70","70 and above"]

                     }
                     ]
            }
            ]
   }

这是我的json数据。我需要在表格视图中显示该数据,如下图所示。

This is the screen shot.我需要以此方式显示。

Currently my code is below.


  func numberOfSections(in tableView: UITableView) -> Int {

        return dummyDataViewModel.numberOfSections()
        print(dummyDataViewModel.numberOfSections())

    }


    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

        let identifier = "DummyDataHeaderCell"


        var headercell: EQ_DummyDataHeader! = tableView.dequeueReusableCell(withIdentifier: identifier) as? EQ_DummyDataHeader

        if headercell == nil {

            tableView.register(UINib(nibName: "EQ_DummyDataHeader", bundle: nil), forCellReuseIdentifier: identifier)

            headercell = tableView.dequeueReusableCell(withIdentifier: identifier) as? EQ_DummyDataHeader

        }

        headercell.setReviewData(reviews:self.dummyDataViewModel.titleForHeaderInSection(atsection:section))

        return headercell

    }

    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {

        if tableView == tableview{

            return 50
        }
        return 20
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return dummyDataViewModel.numberOfRowsIn(section: section)

    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

        let model = dummyDataViewModel.titleForHeaderInSection(atsection: indexPath.section)
        print(model.answerType1)

        print(model.answerType1?.rawValue)

        let c = model.answerType1
        let cellClass = c?.cellType().getClass()
        print(cellClass)
        let cell = tableView.dequeueReusableCell(withIdentifier: (cellClass?.cellReuseIdentifier())!, for: indexPath) as! BaseCell
        print(cell)


        cell.selectionStyle = .none

        let optionModel = dummyDataViewModel.datafordisplay(atindex: indexPath)
        cell.setOptions(Options1: optionModel)

        cell.type = c?.cellType()

        cell.delegate = self
        cell.textdelegate = self
        cell.textdelegate1 = self

        cell.datedelegateDatepicker1 = self

        cell.selectDateDelegate = self
        if optionModel.isSelected! {
            cell.setOptions1(OptionsSelected:optionModel)
            cell.TextDisplay(options:optionModel)


        } else {
            cell.setOptions1(OptionsisSelected:optionModel)
            cell.setOptions(OptionsisSelected:optionModel)
            cell.setOptions1(OptionsisSelected:optionModel)
            cell.TextNotDisplay(options:optionModel)

        }
        print(cell.type)

        if cell.type == .starratingtype{

            cell.selectionStyle = .none
        }
        else if cell.type == .smileytype{

            cell.selectionStyle = .none
        }
        else if cell.type == .checkboxtype{
            cell.selectionStyle = .none
        }
        else if cell.type == .radiotype{
            cell.selectionStyle = .none
        }
        else if cell.type == .textfieldtype{
            cell.selectionStyle = .none
        }

        else if cell.type == .smileytype{
            cell.selectionStyle = .none
        }


        return cell


    }

在视图模型中:-

 func numberOfSections() -> Int{
        print((datasourceModel.dataListArray?.count)!)
        return (datasourceModel.dataListArray?.count)!
    }

    func titleForHeaderInSection(atsection section: Int) -> EQ_DummyDataModel {
        print(datasourceModel.dataListArray![section])
        return datasourceModel.dataListArray![section]
    }

    func numberOfRowsIn(section:Int) -> Int {

        print( datasourceModel.dataListArray?[section].optionsModelArray.count ?? 0)
        return datasourceModel.dataListArray?[section].optionsModelArray.count ?? 0

    }

    func datafordisplay(atindex indexPath: IndexPath) -> EQ_OptionModel{

        print(datasourceModel.dataListArray![indexPath.section].optionsModelArray[indexPath.row])

        return datasourceModel.dataListArray![indexPath.section].optionsModelArray[indexPath.row]
    }

我正在mvvm中进行操作。那么如何在此代码中实现嵌套部分? 在部分和数据源的数量上需要做哪些更改。

0 个答案:

没有答案