【问题标题】:Getting values from plist into my tableView Dictionaries and Arrays从 plist 中获取值到我的 tableView 字典和数组中
【发布时间】:2015-03-08 15:54:00
【问题描述】:

我正在尝试将 Dictionary 键值放入我的 TableView。数据源是一个plist

plist 是这样设置的:很遗憾,我还不能发布图片。

Root = Array
 Item 0 = Dictionary
  modelName = "SomeName"
  modelType = "SomeType"
 Item 1 = Dictionary (similar key : values)
 Item 2 = Dictionary 
 Item 3 = Dictionary 

所以它基本上是一个字典数组。我想遍历数组并在我的 TableView 标签中使用“modelName”值。

当我使用 println 时,我可以打印单个 Array 对象

if let path = NSBundle.mainBundle().pathForResource("models", ofType:    "plist") {
        if let array = NSArray(contentsOfFile: path) {
        self.tableData = array
        println(self.tableData[0])
        }
    }

但我想转换我的数据并调用一个特定的键值以在我的 tableView 中使用。我正在尝试为我的 plist 中的每个项目创建一个字典

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as DataSheetTableViewCell

   var dict = NSDictionary(objectsAndKeys: self.tableData[indexPath.row])
    cell.titleLabel.text = dict["modelName"] as? String

    return cell
}

我对如何创建我的 plist 字典有点困惑。谢谢。

【问题讨论】:

    标签: ios iphone swift plist


    【解决方案1】:

    如果您的 plist 是一个字典数组,那么单个字典只是您使用 objectAtIndex: 获得的数组元素之一(或者使用下标语法等效)。您只需要将其转换为字典,而不需要创建一个新字典,这就是 dictionaryWithObjectsAndKeys 所做的。

    var dict = self.tableData[indexPath.row] as NSDictionary

    【讨论】:

    • 感谢您的解释。我倾向于使这些东西过于复杂。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-14
    • 2015-08-12
    • 1970-01-01
    • 2014-02-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多