首页 > 解决方案 > 检查是否从 UITableViewSection 的每个部分中选择了一个单元格?

问题描述

我需要验证我是否从 UITableViewSection 中选择了一个单元格。

我需要以 mvvm 方式进行。

我的代码如下: -

func numberOfSections(in tableView: UITableView) -> Int {
        return questionViewModel.numberOfSections(tableView: tableView)
    }


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


      //  let headercell = Bundle.main.loadNibNamed("HeaderCell", owner: self, options: nil)?.first as! NH_questionheader


        let identifier = "HeaderCell"

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

        if headercell == nil {
            tableView.register(UINib(nibName: "NH_questionheader", bundle: nil), forCellReuseIdentifier: identifier)
            headercell = tableView.dequeueReusableCell(withIdentifier: identifier) as? NH_questionheader
        }

        headercell.setReviewData(reviews:questionViewModel.titleForHeaderInSection(atsection:section))




        return headercell
    }



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

        return 150

    }



    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // 2
        //return 1

        return questionViewModel.numberOfRowsInSection(section: section)
    }


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


        let identifier = "Cell"
        var cell: NH_QuestionListCell! = tableView.dequeueReusableCell(withIdentifier: identifier) as? NH_QuestionListCell

        if cell == nil {
            tableView.register(UINib(nibName: "NH_QuestionListCell", bundle: nil), forCellReuseIdentifier: identifier)
            cell = tableView.dequeueReusableCell(withIdentifier: identifier) as? NH_QuestionListCell
        }
        cell.contentView.backgroundColor = UIColor.clear


         let optiondatasourcemodel:NH_OptionDataSourceModel = NH_OptionDataSourceModel(array: questionViewModel.tableArray[indexPath.section] as? [String] )
        print(optiondatasourcemodel.dataListArray?.count)



        let optionviewmodel:NH_OptionViewModel = NH_OptionViewModel(withdatasource: optiondatasourcemodel)


        cell.setOptionsData(Options: optionviewmodel.datafordisplay(atindex:indexPath))


        print("Section \(indexPath.section), Row : \(indexPath.row)")

        let modele = questionViewModel.titleForHeaderInSection(atsection: indexPath.section)
        print(modele.buttontype)
        optionviewmodel.button = modele.buttontype

        if (optionviewmodel.button == "1"){

                                cell.imagebutton.setImage(UIImage(named: "checkbox_uncheck.png"), for: .normal)

                            }
                            else if (optionviewmodel.button == "2"){



                                cell.imagebutton.setImage(UIImage(named: "radio_uncheck.png"), for: .normal)
                            }


             return cell
         }













    //Code Cell Selected
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){


        print("Section \(indexPath.section), Row : \(indexPath.row)")





        let model = questionViewModel.titleForHeaderInSection(atsection:indexPath.section)
        print(model.question)

       // reviewViewModel.question = model.id
        print(model.id)


        let value: Int = model.id
        let string = String(describing: value)
        //let x: Int? = Int(model.id)



        questionViewModel.question_id = string
        print(questionViewModel.answer)

       questionViewModel.question = model.question

        let optiondatasourcemodel:NH_OptionDataSourceModel = NH_OptionDataSourceModel(array: questionViewModel.tableArray[indexPath.section] as? [String] )
        print(optiondatasourcemodel.dataListArray?.count)



        let optionviewmodel:NH_OptionViewModel = NH_OptionViewModel(withdatasource: optiondatasourcemodel)

         let cell = tableview.cellForRow(at: indexPath) as? NH_QuestionListCell

         cell?.setOptionsData(Options: optionviewmodel.datafordisplay(atindex:indexPath))

        let optionmodel = optionviewmodel.datafordisplay(atindex:indexPath)
        print(optionmodel.values)

        print("Section \(indexPath.section), Row : \(indexPath.row)")

              let modele = questionViewModel.titleForHeaderInSection(atsection: indexPath.section)
        print(modele.buttontype)
       optionviewmodel.button = modele.buttontype

   //  let cell1 =  tableview.cellForRow(at:indexPath ) as? NH_OptionDataSourceModel


          print("Section \(indexPath.section), Row : \(indexPath.row)")


        print(questionViewModel.questionlist)
        print(questionViewModel.questions)
        print(questionViewModel.answers)


        questionViewModel.question(answer:questionViewModel.question_id!)
        questionViewModel.answer(answer: optionmodel.values!)
        questionViewModel.questionlist(answer: questionViewModel.question!)

        if optionviewmodel.button == "1" {
          // cell?.imagebutton.image(for: .)= UIImage(named: "checkbox_check.png")
            cell?.imagebutton.setImage(UIImage(named: "checkbox_check.png"), for: .normal)
            //tableview.cellForRow(at: indexPath)?.accessoryType = .checkmark
           // self.selected = 1
            questionViewModel.imagename = "1"

        }
//        else if optionviewmodel.button == "1" && self.selected == 1 {
//             cell?.imagebutton.setImage(UIImage(named: "checkbox_uncheck.png"), for: .normal)
//            self.selected = 0
//        }


        else if optionviewmodel.button == "2"{
           // self.selected = 1
            // cell?.imagebutton.imageView?.image = UIImage(named: "radio_check.png")
            cell?.imagebutton.setImage(UIImage(named: "radio_check.png"), for: .normal)
           // tableview.cellForRow(at: indexPath)?.accessoryType = .checkmark
            questionViewModel.imagename = "2"



        }
    }



   func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) {

    let optiondatasourcemodel:NH_OptionDataSourceModel = NH_OptionDataSourceModel(array: questionViewModel.tableArray[indexPath.section] as? [String] )
    print(optiondatasourcemodel.dataListArray?.count)



    let optionviewmodel:NH_OptionViewModel = NH_OptionViewModel(withdatasource: optiondatasourcemodel)

    let cell = tableview.cellForRow(at: indexPath) as? NH_QuestionListCell

    cell?.setOptionsData(Options: optionviewmodel.datafordisplay(atindex:indexPath))

    let optionmodel = optionviewmodel.datafordisplay(atindex:indexPath)
    print(optionmodel.values)


    let modele = questionViewModel.titleForHeaderInSection(atsection: indexPath.section)
    print(modele.buttontype)
    optionviewmodel.button = modele.buttontype



     print(optionmodel.values)


     questionViewModel.removequestion(question:questionViewModel.question!)
     questionViewModel.removeanswer(answer:optionmodel.values!)
     questionViewModel.removequestionid(removequestionid:questionViewModel.question_id!)




    print(questionViewModel.question_id)


    print(modele.question)

    // reviewViewModel.question = model.id
    print(modele.id)


    let value: Int = modele.id
    let string = String(describing: value)
    //let x: Int? = Int(model.id)


//    if questionViewModel.question_id == "\(self.questionViewModel.questions)" {
//       
//    }
//    
//    
//    questionViewModel.question_id = string
//    
//    questionViewModel.question = modele.question
//    
//     questionViewModel.answer = optionmodel1.values



    if optionviewmodel.button == "1"{
       //  cell?.imagebutton.imageView?.image = UIImage(named: "checkbox_uncheck.png")
        cell?.imagebutton.setImage(UIImage(named: "checkbox_uncheck.png"), for: .normal)


        // tableview.cellForRow(at: indexPath)?.accessoryType = .none
        self.selected = 1
        questionViewModel.imagename = "1"

    }

    else if optionviewmodel.button == "2"{
        self.selected = 1
        // cell?.imagebutton.imageView?.image = UIImage(named: "radio_uncheck.png")
        cell?.imagebutton.setImage(UIImage(named: "radio_uncheck.png"), for: .normal)

        // tableview.cellForRow(at: indexPath)?.accessoryType = .none
        questionViewModel.imagename = "2"

    }


    }

我只需要从 UITableViewSection 中选择一个单元格。但是从每个部分中我需要选择一个单元格。

如何验证我是否从 UITableviewSection 的每个部分中选择一个单元格。如何在 Viewmodel 中有效?

标签: ios

解决方案


推荐阅读