我正在尝试获取一个 plist 来填充 uitableview
plist 看起来像这样:
我遇到问题的代码:
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/