2016-05-26 14 views
2

プログラミングを開始しましたSwiftと私は解決方法がわからないという問題があります。配列を辞書に表示する方法 - Swift

私はplistファイル製:今、私は各アレイ名(キー)になるようにそのルート(辞書)でTableViewのすべての配列(果物や野菜)を表示したい

 <dict> 
<key>Vegetables</key> 
<array> 
    <string>Pepper</string> 
    <string>Tomato</string> 
    <string>Cucumber</string> 
</array> 
<key>Fruits</key> 
<array> 
    <string>Apple</string> 
    <string>Banana</string> 
    <string>Watermelon</string> 
</array> 
     </dict>  

を別の セクションとすべての文字列は私のTableViewの単一の行になります。

ここは私のビューコントローラクラスである:彼らは順不同であるため、

import UIKit 
    //I add TableView methods 

    class ViewController : UIViewController , UITableViewDelegate ,UITableViewDataSource { 

    override func viewDidLoad(){    
    } 

    func numberOfSectionsInTableView(tableView: UITableView) -> Int { 

    // return number of section in my case it's two section (fruits and vegetables) 
     return 1; 
    } 

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    // number of row in every section is three 
    // I want to do something like Array.count 
    return 1; 
    } 
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

    let row : UITableViewCell = UITableViewCell(); 
    //Add every Row 
    return row;  
    } 

    func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 

    // Here i want to add header to every section i made; 
    // Header Name = Array (key) Fruit/Vegetables 
    // How i do this ? 
    return ""; 
    } 
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) { 
     } 
     } 
+1

最初に、plistデータの単一セクションでtableviewを作成する方法を知っておいてください.http://www.ioscreator.com/tutorials/load-data-property-list-ios8-swiftこれで、マイナー変更、あなたが単一のセクションを持つtableviewの人口で完了し、任意の難しさを感じると、いくつかのコードで戻ってくる。あなたはコミュニティからの解決策に落ちることができますが、学習の美しさはそれを行うことによって学びます。あなたはいつもあなたが過ごしたことを覚えています。 –

答えて

0

コード以下のような書き込みや、あなたの答えを得るでしょう、

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource 
{ 
var listDic : NSMutableDictionary = NSMutableDictionary() 
var headerArr : NSMutableArray = NSMutableArray() 

@IBOutlet var tblList : UITableView! 

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 
    self.loadData() 
    self.navigationController?.navigationBar.translucent = true 
    self.navigationController?.navigationBar.tintColor = UIColor.grayColor() 

} 

func loadData() 
{ 
    if let path = NSBundle.mainBundle().pathForResource("test", ofType: "plist") // Rename with your plist file name 
    { 
     self.listDic = NSMutableDictionary(contentsOfFile: path)! // NSMutableArray(contentsOfFile: path)! 
    } 

    if(self.listDic.count > 0) 
    { 
     self.headerArr.addObjectsFromArray(self.listDic.allKeys) 
    } 

    self.tblList.reloadData() 
} 

func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    return self.headerArr.count 
} 

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

    let arr = self.listDic.valueForKey(self.headerArr.objectAtIndex(section) as! String) as! NSMutableArray 
    return arr.count 
} 

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 
    return self.headerArr.objectAtIndex(section) as? String 
} 


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

    let cell = UITableViewCell(style: .Default, reuseIdentifier: "Cell") 

    let arr = self.listDic.valueForKey(self.headerArr.objectAtIndex(indexPath.section) as! String) as! NSMutableArray 

    cell.textLabel?.text = arr.objectAtIndex(indexPath.row) as? String 
    return cell 
} 
} 

は、これはあなたのplistファイルと作業罰金に基づいています。

希望すると、これが役立ちます。

+0

ありがとうございました! –

+0

ええ、歓迎です:) –

2

辞書には、テーブルビューのデータソースであることが適していません。あなたのセクションを含む外側の配列を作成することをお勧めします。各セクションについて、タイトルとアイテムの配列を含む辞書を追加します。

タイトルと項目エントリを持つカスタムセクション構造体を作成し、セクション構造体の配列を作成することもできます。

+0

答えをありがとう、私はサイトでは新しいので、あなたのために投票することはできませんが、私の仕事はplistでそれを作ることでした –

+0

あなたのplistをリファクタリングして、外側のコンテナが配列セクションには、セクションタイトルとアイテムの配列が含まれています。 –

関連する問題