2016-09-22 23 views
1

値:私の第二ビューコントローラのテーブルビュー」FUNCで解析辞書と私はこのような辞書持っ

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) { 

//I want to print "A", "B", "C" here. 

} 

:今、私は第一ビューコントローラのテーブルビューのFUNCを持って

var mapNames: [String: [String]] = 
    [ 
      "A": ["A1", "A2"], 
      "B": ["B1"], 
      "C": ["C1", "C2", "C3"] 
    ] 

を:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    //If user taps on "A" in the 1st VC, show here cells with "A1", "A2" 

} 

私の辞書を解析して、最初にキーのリストを取得してから各キーy、対応する値のリストを取得しますか?

ありがとうございました。

+1

ソースから学ぶ:https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/CollectionTypes.html#//apple_ref/doc/uid/TP40014097-CH8-ID113 – Moritz

答えて

0

、あなたがドキュメントを最初にお読みください。配列は各key、使用に対応する取得する今

let keys = mapNames.keys 
//keys is the array you need on 1st view controller 

を使用し、辞書からすべてのキーを取得するには

for (key, value) in mapNames { 
    print(key) 
    print(mapNames[key]) 
} 
0

DictionaryArrayに分割してください。

例:エリックで指摘したように

var mapNames: [String: [String]] = 
[ 
    "A": ["A1", "A2"], 
    "B": ["B1"], 
    "C": ["C1", "C2", "C3"] 
] 

var mapNamesKeys = mapNames.keys 

var mapNamesValues = mapNames.values 
0

: - キーを取得するには、辞書の値を、あなたはこれを行う必要があります

for key in keys 
{ 
    let value = mapNames[key] 
    //value is the array you need on 2nd view controller corresponding to the selected key 
} 
関連する問題