【问题标题】:Populate a UITableView with plist使用 plist 填充 UITableView
【发布时间】: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


    【解决方案1】:

    所有这些代码都粘贴在 viewDidLoad 中:

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

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

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

    也适用于 dict!。

    希望我能帮上忙 :)

    更新:

        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;
    }
    

    它现在对我有用。

    【讨论】:

    • 嗯,如果我将其移至 viewDidLoad,其中包含 dict 的每个项目都会开始说“GravidMenu 没有名为 dict 的成员”
    • 更新了帖子以反映更改。这次贴出完整代码
    • 然而!在 cell.labeltext.text 下现在不起作用。
    • 通过将 var txt 移出 viewDidLoad 使其正常工作。现在如何在 var txt 中说 dict 不是 GravidMenu 的成员。刚刚发布它作为答案。我会更新代码以反映
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多