从 Eureka 表单的多值部分获取表单值

Get Form Values from Multivalued sections of Eureka Form

在这里问这个问题是因为文档中还没有涉及到这个问题,他们会监控并回答这个标签。 我正在使用 Eureka 构建多值表单。 这是我的代码:

    +++ MultivaluedSection(multivaluedOptions: [.Reorder, .Insert, .Delete],
        header: "Options",
        footer: "footer") {
                            [=10=].addButtonProvider = { section in
                                return ButtonRow(){
                                    [=10=].title = "Add New Option"
                                }
                            }
                            [=10=].multivaluedRowToInsertAt = { index in
                                print(self.form.values())
                                return NameRow() {
                                    [=10=].placeholder = "Your option"
                                }
                            }
                            [=10=] <<< NameRow() {
                                [=10=].placeholder = "Your option"
                            }
    }

现在我想在最后提取 NameRows 中的所有值。可以有任意数量的行(基于用户输入)。这是我试过的:

self.form.values() 但结果是 [ : ].

如何获取所有值?

对于有类似问题的任何人:

form.values() 为空,因为没有为行指定标签。 要从 form 中获取值,请为行提供标签,您将获得一个包含键作为这些标签的值的字典。对于这种情况

+++ MultivaluedSection(multivaluedOptions: [.Reorder, .Insert, .Delete],
                           header: "header",
                           footer: "footer") {
                            [=10=].addButtonProvider = { section in
                                return ButtonRow(){
                                    [=10=].title = "Button Title"
                                }
                            }
                            [=10=].multivaluedRowToInsertAt = { index in
                                return NameRow("tag_\(index+1)") {
                                    [=10=].placeholder = "Your option"
                                }
                            }
                            [=10=] <<< NameRow("tag_1") {
                                [=10=].placeholder = "Your option"
                            }
    }

现在,对于用户插入的任意数量的行,这些值将作为 ["tag_1" : "value 1", "tag 2 : "value 2" ....] 返回。
P.S.: 在标签中使用了 index 因为不允许重复的标签并且 index 值对于不同的行是不同的。

form.values() returns 词典,而词典没有订单信息。
所以你不能从 form.values() 得到重新排序的值。

我得到了如下的有序数据:
(form.allRows returns 数组,其中有订单信息。)

// Dictionary doen't have order info.
let values: Dictionary = form.values()
NSLog("LogA : %@", values)

// Array has order info, and this can return all tags.
let allRow: Array<BaseRow> = form.allRows
for i in 0..<allRow.count {
    let tmp: BaseRow = allRow[i]
    NSLog("LogB : %d : %@", i, tmp.tag ?? "")
}

// So make ordered data by from.values() and form.allRows like this.
var results: Array = [String]()
for i in 0..<allRow.count {
    let tag: String = allRow[i].tag ?? ""
    if(tag != ""){
        let val: String = values[tag] as! String
        results.append(tag + ":" + val)
    }
}
NSLog("LogC = %@", results)

谢谢

如所述 只需将标签赋予每一行;

<<< NameRow() {
[=10=].tag = "NameRow"
[=10=].title = "Name:"
[=10=].value = "My name" }

这个方法简单多了

首先,给整个Multivalued Section对象添加一个标签。

    +++ MultivaluedSection(multivaluedOptions: [.Reorder, .Insert, .Delete],
    header: "Options",
    footer: "footer") {

                        [=10=].tag = "someTag" // added this line

                        [=10=].addButtonProvider = { section in
                            return ButtonRow(){
                                [=10=].title = "Add New Option"
                            }
                        }
                        [=10=].multivaluedRowToInsertAt = { index in
                            print(self.form.values())
                            return NameRow() {
                                [=10=].placeholder = "Your option"
                            }
                        }
                        [=10=] <<< NameRow() {
                            [=10=].placeholder = "Your option"
                        }
}

然后,您可以通过

获取值
let values = (self.form.values()["someTag"]!! as! [Any?]).compactMap { [=11=] }