вложенные разделы в табличном виде в Swift - PullRequest
0 голосов
/ 22 ноября 2018
{
    "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. Мне нужно отобразить данные в виде таблицы, как показано на снимке экрана ниже.

Это снимок экрана. Мне нужно отобразить в этомway.

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. Так как реализовать вложенный раздел в этом коде?Какие изменения необходимо сделать в количестве разделов и источника данных.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...