如何获取带有自定义对象的Eureka .multiSelectedList的Swift4选择值并设置默认值

时间:2018-07-24 13:14:46

标签: swift4 eureka-forms

情况:

  • 我使用Swift 4,Xcode 9和“ Eureka表单库”
  • 我有一个带有.singleSelection的列表,效果很好(代码如下)
  • 我使用视图模型来获取/传递值到列表(列表元素和选定的元素)

我想做什么:

  • 当我使用“ .multipleSelection”时,我希望能够设置默认的选定元素(例如第一个和第三个元素)
  • 单击按钮时,我要从列表中打印选定的值

代码:

import UIKit
import Eureka

struct ComboType : Equatable {
    let nume : String
    let id : Int
}

class clsModel_form_continente{
    var v_combo: [ComboType] = []
    var continent_lista_selectat: ComboType!
     init(_ v_combo: [ComboType]){
        self.v_combo = v_combo
    }
}

class pagina_lista: FormViewController {
    var model_formular1: clsModel_form_continente!
    var combo_selectata: ComboType!
    var v_combo: [ComboType]!

    override func viewDidLoad() {
        super.viewDidLoad()

         v_combo = [ComboType(nume: "Any", id: 0), ComboType(nume: "Antarctica", id: 10), ComboType(nume: "Africa", id: 50),  ComboType(nume: "Asia", id: 75)]
        combo_selectata = ComboType(nume: "Any", id: 0)

        model_formular1 = clsModel_form_continente(v_combo); model_formular1.continent_lista_selectat = combo_selectata

        create_form2()

       //default selected value in my list
        let defaultContinent = combo_selectata
        if let row = self.form.rowBy(tag: (defaultContinent?.nume)!) as? ListCheckRow<ComboType> {
            row.selectableValue = defaultContinent
            row.didSelect()
        }

    }

    func create_form2(){
           form +++ SelectableSection<ListCheckRow<ComboType>>("Continents", selectionType: .singleSelection(enableDeselection: false)) // **here I want to use .multipleSelection**
        {section in
            section.tag = "ID_LISTA"
        }
        for element in v_combo {
            form.last! <<< ListCheckRow<ComboType>(){ listRow in
                if element.id == 0 //i make first row green
                {
                   listRow.cell.backgroundColor = UIColor.green
                }

                listRow.title = element.nume
                listRow.selectableValue = element
                listRow.value = element
                listRow.tag = element.nume
                listRow.onChange{[unowned self] row in
                     if let valoare = row.value{
                        self.combo_selectata  = valoare
                        self.model_formular1.continent_lista_selectat = valoare
                     }
                }
            }
        }

        form.last! <<< ButtonRow("Button1") {row in
            row.title = "Get List Value"
            row.onCellSelection{[unowned self] cell, row in // clik on button

                if let section = self.form.sectionBy(tag: "ID_LISTA") as?
                    SelectableSection<ListCheckRow<ComboType>> {


                    //read data from my list
                    if let valoare_form = section.selectedRow()?.value  {
                        print("From my form .... Name: ",valoare_form.nume, " Id: ",valoare_form.id)
                    }else{
                      print( "Nothing is selected")
                    }

                    //read data from my object
                    print("From my  Object ....... Name = ",self.model_formular1.continent_lista_selectat.nume," , Id = ", self.model_formular1.continent_lista_selectat.id)
                    print("\n")
                }
            }
        }

    }
}

0 个答案:

没有答案