簡體   English   中英

在帶有indexPath.row的tableView中從Dictionary獲取值?

[英]get value from Dictionary in a tableView with an indexPath.row?

我正在嘗試從plist獲取信息。 這是結構:

Root
-Dictionary
--Dictionary
---String
---String
--Dictionary
---String
---String
--Dictionary
---String
---String

編輯:和它的一些示例:

<plist version="1.0">
<dict>
    <key>introduction</key>
    <dict>
        <key>gfx</key>
        <dict>
            <key>titre</key>
            <string>Introduction</string>
            <key>color</key>
            <string></string>
            <key>miniLogo</key>
            <string></string>
            <key>bigLogo</key>
            <string></string>
        </dict>
...

所以基本上我是從嵌套在另一個字典中的字典中獲取字符串值的。

訣竅是,我想使用tableView

到目前為止,我在這里:

let pathRoot = NSBundle.mainBundle().pathForResource("MyPLIST", ofType: "plist")

let rootRubrique = NSDictionary(contentsOfFile: pathRoot!)

我正在嘗試獲取這樣的信息:

let rubriqueTitre = NSDictionary(contentsOfFile: pathRoot!)?.allKeys[indexPath.row].objectForKey("gfx")?.valueForKey("titre")

但也許問題出在我嘗試篩選的方式上

label.text = imageRubrique! as! String
// And also tried :
label.text = "\(imageRubrique!)"

我也嘗試獲得其他值,只是嘗試。 這為我工作:

let rubriqueIndex = NSDictionary(contentsOfFile: pathRoot!)?.allKeys[indexPath.row]
 label.text = rubriqueIndex as! String

我得到了一個小寫的“介紹”,第一個。 但是我不想要那個值,我想要嵌套的值! 我快瘋了! 對我來說幸運的是Stackoverflow存在!

之后,我將必須找到一種方法以正確的順序對字典進行排序,這似乎是另一個大問題……

編輯:我解決了這樣的第一個問題。 我已經創建了這個func:

func getValueFromPLIST(rub: String, sousDossier : String, sousSousDossier : String?, value : String) -> String {

if sousSousDossier == nil {

    return (NSDictionary(contentsOfFile: pathRoot!)!.objectForKey(rub)!.objectForKey(sousDossier)!.valueForKey(value) as? String)!
} else {

    return (NSDictionary(contentsOfFile: pathRoot!)!.objectForKey(rub)!.objectForKey(sousDossier)!.objectForKey((sousSousDossier!))!.valueForKey(value) as? String)!
}

}

然后我將索引分配給一個常量:

let rubriqueIndex = NSDictionary(contentsOfFile: pathRoot!)?.allKeys[indexPath.row]

然后我通過func生成整個valueForKey:

let rubriquePathTitre = getValueFromPLIST(String(rubriqueIndex!), sousDossier: "gfx", sousSousDossier: nil, value: "titre")

我不知道為什么它可以使用該技巧,但是它可以使用,而且還不錯,因為我需要在其他地方使用索引。 但是不幸的是,我認為我將不得不使用其他東西,例如“ rank”或“ ID”值來對字典進行排序。 所以整個事情到底是沒用的哈哈!

您可以使用UITableView索引代替迭代索引

// *** Access data from plist ***
let pathRoot = NSBundle.mainBundle().pathForResource("MyPLIST", ofType: "plist")

// *** Get Root element of plist ***
let rootRubrique = NSDictionary(contentsOfFile: pathRoot!)

// *** get Next element which is `Introduction` holding all other objects ***
let objIntro:NSDictionary = rootRubrique!.objectForKey("introduction") as! NSDictionary

選項1:使用索引遍歷所有對象

for var i = 1; i <= objIntro.allValues.count; ++i
{
    print("Index[\(i)] \(objIntro.allValues[0])")
}    

在您的情況下使用索引路徑訪問

let obj = (objIntro.allValues[indexPath.row])

選項2:遍歷字典的allValues

for (key, value) in objIntro
{
    print("key: \"\(key as! String)\" obj : \(value)")
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM