【问题标题】:Get Form Values from Multivalued sections of Eureka Form从 Eureka 表单的多值部分获取表单值
【发布时间】:2017-04-23 20:45:06
【问题描述】:

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

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

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

self.form.values() 但它会导致[ : ]

如何获取所有值?

【问题讨论】:

    标签: ios swift eureka-forms


    【解决方案1】:

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

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

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

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

    【讨论】:

      【解决方案2】:

      这个方法更简单

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

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

      然后,您可以通过

      let values = (self.form.values()["someTag"]!! as! [Any?]).compactMap { $0 }
      

      【讨论】:

      • 我收到此错误:无法推断通用参数“ElementOfResult”
      【解决方案3】:

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

      我得到了如下的有序数据:
      (form.allRows 返回 Array,并且有订单信息。)

      // 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)
      

      谢谢

      【讨论】:

        【解决方案4】:

        here 所述 只需为每一行添加标签即可;

        <<< NameRow() {
        $0.tag = "NameRow"
        $0.title = "Name:"
        $0.value = "My name" }
        

        【讨论】:

        • 上面的答案中描述了同样的事情
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多