简体   繁体   English

如何在字典中显示数组 - Swift

[英]How to display the array in dictionary - Swift

I started programming Swift and I have a problem that I don't know how to solve.我开始对Swift进行编程,但遇到了一个我不知道如何解决的问题。

I made a Plist File :我做了一个Plist 文件

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

Now I want to display all Arrays(Fruits and Vegetables) that in the root (Dictionary) in TableView so that each Array name(Key) will be a separate Section and every String will be a single row in my TableView .现在我想在TableView的根(字典)中显示所有数组(水果和蔬菜),这样每个数组名称(键)将是一个单独的部分,每个字符串将是我的TableView一行。

Here is my view controller class:是我的视图控制器类:

    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) {
      }
      }

Dictionaries are not well suited to be the data source for table views because they are unordered.字典不太适合作为表视图的数据源,因为它们是无序的。 I suggest you create an outer array that contains your sections.我建议您创建一个包含您的部分的外部数组。 For each section, add a dictionary that contains a title, and an array of items.对于每个部分,添加一个包含标题和项目数组的字典。

Alternately you could create a custom section struct that has a title and an items entry, and create an array of section structs.或者,您可以创建一个具有标题和项目条目的自定义部分结构,并创建一个部分结构数组。

Write like below code and you will get your answer,像下面这样写代码,你会得到答案,

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
}
}

This is based on your plist file and working fine.这是基于您的 plist 文件并且工作正常。

Hope this will help you.希望这会帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM