如何从带有节的表视图的嵌套字典中获取数据?

时间:2016-12-16 16:02:21

标签: uitableview nsdictionary plist

我想用自定义plist文件的内容填充表格视图。表视图必须分段。我在Xcode 8中工作并在Swift中编写代码。

我有一个像这样的plist:

    <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Item0</key>
    <dict>
        <key>SectionKey</key>
        <dict/>
    </dict>
</dict>
</plist>

在网上我发现了一个很好的描述如何创建一个适合我的分段表视图:Segmented Table View Tutorial

此示例使用如下结构:

struct Section {

var heading: String
var items: [String]

init(title: String, objects: [String]) {

    heading = title
    items = objects
}

}

然后它用这个函数中的数据填充结构:

func getSectionsFromData() -> [Section] {

    var sectionsArray = [Section]()

    let animals = Section(title: "Animals", objects: ["Cats", "Dogs", "Birds", "Lions"])
    let vehicles = Section(title: "Vehicles", objects: ["Cars", "Boats", "Planes", "Motorcycles", "Trucks"])

    sectionsArray.append(animals)
    sectionsArray.append(vehicles)

    return sectionsArray
}

所以,我的想法是遍历我的plist(我将它存储在一个名为dict的NSMutableDicttionary中)以某种方式获取数据,我可以使用这个例子来以正确的方式填充我的表视图:

for i in (0..<dict.count) {

        newDict = dict.object(forKey: "Item\(i)") as! NSMutableDictionary
    }

我在newDict中获得的是一个看起来像这样的结构(这些是我的三个部分:账户,信用卡和现金)。所有键和值都是字符串:

    {
    "Account" =     (
                {
            AmountKey = "Some amount";
            CategoryKey = Some category;
            DateKey = "Some date";
        }
    );
    "Credit card" =     (
                {
            AmountKey = "Some amount";
            CategoryKey = Some category;
            DateKey = "Some date";
        }
    );
    "Cash" =     (
                {
            AmountKey = "Some amount";
            CategoryKey = Some category;
            DateKey = "Some date";
        }
    );
}

问题是:我如何遍历我的dict,获取所有条目按帐户,信用卡和现金分类,以便我可以使用上面的例子(而不是:let animals = Section(title:&#34) ;动物&#34;,对象:[&#34; Cats&#34;,&#34; Dogs&#34;,&#34; Birds&#34;,&#34; Lions&#34;])我得到类似的东西这:let account = Section(标题:&#34;帐号&#34;,对象:[[&#34; AmountKey&#34;:&#34;一些金额&#34;,&#34; CategoryKey&#34;: &#34;某些类别&#34;,&#34; DateKey&#34;:&#34;某些日期&#34;],[&#34; AmountKey&#34;:&#34;一些金额&#34;, &#34; CategoryKey&#34;:&#34;某些类别&#34;,&#34; DateKey&#34;:&#34;某些日期&#34;],..]。

感谢马丁

0 个答案:

没有答案
相关问题