Search code examples
swift4eureka-forms

How to get the selected value and set a default value in a list with EurekaForm Library / Swift


I use xCode 9, Swift 4 and "Eureka form library" for my project.

The situation :

I have form with a list and a button.

I need help with these 2 issues :

  1. when click on button I want to print the selected value
  2. I want to be able to set for the list an element as a default selected value

My code :

import UIKit
import Eureka

class myPage: FormViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        createForm()
    }


    func createForm(){
        form
        +++ Section("Sample list ")
        form +++ SelectableSection<ListCheckRow<String>>("Continents", selectionType: .singleSelection(enableDeselection: false))

        let continents = ["Africa", "Antarctica", "Asia", "Australia", "Europe", "North America", "South America"]

        for element in continents {
            form.last! <<< ListCheckRow<String>(element){ listRow in
                listRow.title = element
                listRow.selectableValue = element
                listRow.value = nil
            }
        }

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

            print ("Selected List Value = ????????")
        }
    }
}

Thanks in advance.


Solution

  • For printing all the form value:

    print(form.values())
    

    This will print the dictionary for the all form values keyed by the row tag.

    For this case it prints like this (Australia is selected):

    ["Asia": nil, "Africa": nil, "Antarctica": nil, "Australia": Optional( "Australia"), "Europe": nil, "South America": nil, "Button1": nil, "North America": nil]

    Eureka's SelectableSection also have selectedRow() (for multiple selection selectedRows()) methods.

    So you can get selected values like this:

    First just add tag to SelectableSection to a tag.

    form +++ SelectableSection<ListCheckRow<String>>("Continents", selectionType: .singleSelection(enableDeselection: false)) { section in
       section.tag = "SelectableSection"
    }
    

    Now on the button selection

    form <<< ButtonRow("Button1") { row in 
            .. // button setup
        }.onCellSelection { [unowned self] (cell, row) in
            if let section = self.form.section(by: "SelectableSection") as?
                                   SelectableSection<ListCheckRow<String>> {
                print(section.selectedRow()?.value ?? "Nothing is selected") 
            }
        }
    

    Now For Default value selection:

    let defaultContinent = "Antarctica" 
    

    Now in Button's onCellSelection:

    }.onCellSelection { [unowned self] (cell, row) in
        .. // printing the selected row as above
        if let row = self.form.row(by: defaultContinent) as? ListCheckRow<String> {
           row.selectableValue = defaultContinent 
           row.didSelect()
        }
    }