class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let path = NSBundle.mainBundle().pathForResource("TableRowInfo", ofType: "plist")!
let dict = NSDictionary(contentsOfFile:path)!
var artists: AnyObject = dict.objectForKey("Artist")!
var stages: AnyObject = dict.objectForKey("Stage")!
println(artists)
println(stages)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return artists.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier("InfoCell", forIndexPath: indexPath) as? UITableViewCell
if cell == nil {
cell = UITableViewCell(style: .Subtitle, reuseIdentifier: "InfoCell")
cell!.accessoryType = .DisclosureIndicator
}
return cell!
}
}
大家好,
我是 swift 的新手,所以只是尝试了一些东西。我想做的是用 plist 文件中的内容填充我的表。我知道这不是最好的方法!我已经成功加载了列表。我的 println(artists) 返回我想要的以及阶段。唯一的问题是,如果我在 viewDidLoad 函数之外调用艺术家或舞台,它就不起作用。为什么会这样,我该如何解决?
提前致谢。
问候,
沃特
最佳答案
变量“artists”和“stages”不存在于 viewDidLoad-Function 范围之外。您必须将它们定义为属性才能在 viewDidLoad 函数之外访问它们。像这样
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var artists: AnyObject?
var stages: AnyObject?
override func viewDidLoad() {
...
artists: AnyObject = dict.objectForKey("Artist")
...
}
关于ios - 快速从viewdidload调用对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29098797/