uitableview - 使用 plist 填充 UITableView

标签 uitableview plist swift

我正在尝试获取一个 plist 来填充 uitableview

plist 看起来像这样: enter image description here

我遇到问题的代码:

class GravidMenu: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet
var tableView: UITableView
var dict = NSMutableArray(contentsOfFile: NSBundle.mainBundle().pathForResource("links", ofType: "plist"))
var txt : NSString = dict.valueForKey("menuPunkt") as NSString //this throws a error : GravidMenu.Type does not have a member named dict


func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
    return self.dict.count;
}

func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
    var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
    if let row = indexPath?.row {
        
        cell.textLabel.text = self.txt[row] // throws a error Nsstring does not have a a member named subscript
        println("Text \(txt)")
    }
    
    return cell
}

似乎我越来越近了。


更新

显示完整代码 调整 cell.label 和 var txt。还将它的一些代码移动到 viewDidLoad,这似乎破坏了它

import UIKit
import Foundation


class GravidMenu: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet
var tableView: UITableView

override func viewDidLoad() {
    super.viewDidLoad()
    var dict = NSMutableArray(contentsOfFile: NSBundle.mainBundle().pathForResource("links", ofType: "plist"))
    var txt : NSArray = dict.valueForKey("menuPunkt") as NSArray
    
    self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")

}


func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
    return self.dict.count; // Throws Error GravidMenu does not have a member named dict
}

func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
    var cell:UITableViewCell = self.tableView.dequeueReusableCellWithIdentifier("cell") as UITableViewCell
    if let row = indexPath?.row {
        
        cell.textLabel.text = "\(txt.objectAtIndex(indexPath.row))"
        println("Text \(txt)")
    }
    
    return cell
}

func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!) {
    println("You selected cell #\(indexPath.row)!")
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}


}

更新

let dict : NSMutableArray! = NSMutableArray(contentsOfFile: NSBundle.mainBundle().pathForResource("links", ofType: "plist"))
var txt : NSArray! = dict?.valueForKey("menuPunkt") as? NSArray!

这给我带来了麻烦。它一直在 var txt 行中说“GravidMenu 没有名为 dict 的成员”……有人吗?帮助

最佳答案

将所有代码粘贴到 viewDidLoad 中:

var dict : NSMutableArray = NSMutableArray(contentsOfFile: NSBundle.mainBundle().pathForResource("links", ofType: "plist"))
var txt : NSString = dict!.valueForKey("menuPunkt") as NSString

我会搜索你为什么要在viewDidLoad中写的原因。

我建议你打个问号,因为你不知道它是否会是 nil,(如果找不到文件)

还有听写!

希望我有所帮助:)

更新:

    let dict : NSMutableArray! = NSMutableArray(contentsOfFile: NSBundle.mainBundle().pathForResource("links", ofType: "plist"))

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    var txt : NSString = dict!.valueForKey("menuPunket") as NSString;
}


func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
    return self.dict!.count;
}

现在对我有用。

关于uitableview - 使用 plist 填充 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24097653/

相关文章:

iphone - 无论我尝试什么,CFBundleDisplayName 的本地化都不起作用

ios - 什么是我的应用程序的更好选择 - CoreData 或 plist?

ios - 如何在 Swift 中设置新的类文件

swift - 添加嵌套字典导致 JSONSerialization 返回 nil

ios - 使用 NSUserDefaults 将字符串数组保存到另一个 UITableView

ios - 单元格背景不透明

xcode - 删除 heightForRowAtIndexPath : compatibility with iOs7

iphone - 在类似于 iHome Connect 的 UITableView 中显示可用的 wifi

ios - 如何使用 NSKeyedArchiver(或其他机制)以 XML 格式将自己的类列表保存在 plist 中?

ios - 有什么方法可以创建不公开源代码的用 Swift 编写的库或框架吗?