【发布时间】:2014-06-07 13:20:06
【问题描述】:
我正在尝试获取一个 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 的成员”......有人吗?帮助
【问题讨论】:
标签: uitableview plist swift