2017-04-23 117 views
3

在此處提出此問題,因爲尚未在文檔中介紹此問題,並且他們監視並回答此標記。 我使用Eureka來構建多值表單。 這是我的代碼:從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() 但它的結果是[ : ]

如何獲取所有值?

回答

5

對於任何類似的問題:

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值對於不同的行是不同的。

0

form.values()返回Dictionary,而Dictionary沒有訂單信息。
所以你不能從form.values()得到Reordered值。

我得到了orderd數據如下圖所示:
(form.allRows返回數組,並有秩序的信息。)

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

感謝

0

如上所述here 只需要給標籤的每一行;

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

在上面的答案中描述了相同的事情 – sasquatch