想在 NStableview 中打开一个 plist

Want open a plist in NStableview

几天来我一直在尝试在 NSTableView 中显示一个 plist 文件。我读了很多书,但到目前为止只找到 iOS 的说明。我的问题是个别值在NStableView中没有单独显示。始终只显示单元格中的整个 plist 文件。

我希望有人能帮助我,因为我不能再进一步了。我还是 Swift.

的新生

我上传了我的Plist,如果有人能告诉我错误在哪里。

https://www.dropbox.com/s/1t8c4uldwdtp3wk/Archiv.zip?dl=0

import Cocoa


class ViewController: NSViewController, NSTableViewDelegate, NSTableViewDataSource {

@IBOutlet weak var tableView: NSTableView!

@IBOutlet weak var ntext: NSTextField!
@IBOutlet weak var schwarztext: NSTextField!
@IBOutlet weak var cyantext: NSTextField!
@IBOutlet weak var magentatext: NSTextField!
@IBOutlet weak var gelbtext: NSTextField!
@IBOutlet weak var lightcyantext: NSTextField!
@IBOutlet weak var lightmtext: NSTextField!

var n : String? = nil; // n
var schwarz : String? = nil; // Schwarz
var cyan : String? = nil; // Cyan
var magenta : String? = nil; // Magenta
var gelb : String? = nil; // Gelb
var lightc : String? = nil; // Light Cyan
var lightm : String? = nil; // Light Magenta

let AUTO_PLIST_JOBS_PATH = Bundle.main.path(forResource: "Jobs", ofType: "plist")

let AUTO_PLIST_JOBS_KEY_N = "n"   // n
let AUTO_PLIST_JOBS_KEY_SCHWARZ = "schwarz"   // Schwarz
let AUTO_PLIST_JOBS_KEY_CYAN = "cyan"   // Cyan
let AUTO_PLIST_JOBS_KEY_MAGENTA = "magenta"   // Magenta
let AUTO_PLIST_JOBS_KEY_GELB = "gelb"   // Gelb
let AUTO_PLIST_JOBS_KEY_LIGHTC = "lightc"   // Light Cyan
let AUTO_PLIST_JOBS_KEY_LIGHTM = "lightm"   // Light Magenta

var _jobss = [Jobs]()

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.delegate = self
    tableView.dataSource = self

    // Do any additional setup after loading the view.


}

func tableView(_ tableView: NSTableView, objectValueFor tableColumn: NSTableColumn?, row: Int) -> Any? {

    //let path = Bundle.main.path(forResource: "Jobs", ofType: "plist")
    //let myJobs100 = NSArray(contentsOfFile: path!)
    //return myJobs100


    let path = Bundle.main.path(forResource: "Jobs", ofType: "plist")
    let url = URL(fileURLWithPath: path!)
    let data = try! Data(contentsOf: url)
    let plist = try! PropertyListSerialization.propertyList(from: data, options: .mutableContainers, format: nil)

    let dictArray = plist as! [[String:String]]
    return dictArray
}


func allJobss() -> [Jobs]{

    if _jobss.count > 0 {
        return _jobss
    }

    if let allDatas = NSArray(contentsOfFile: AUTO_PLIST_JOBS_PATH!) {

        for dict in allDatas {
            guard let dict = dict as? [String: AnyObject] else {continue}

            let jobs = Jobs()
            jobs.n = dict[AUTO_PLIST_JOBS_KEY_N] as? String // n
            jobs.schwarz = dict[AUTO_PLIST_JOBS_KEY_SCHWARZ] as? String // Schwarz
            jobs.cyan = dict[AUTO_PLIST_JOBS_KEY_CYAN] as? String // Cyan
            jobs.magenta = dict[AUTO_PLIST_JOBS_KEY_MAGENTA] as? String // Magenta
            jobs.gelb = dict[AUTO_PLIST_JOBS_KEY_GELB] as? String // Gelb
            jobs.lightc = dict[AUTO_PLIST_JOBS_KEY_LIGHTC] as? String // Light Cyan
            jobs.lightm = dict[AUTO_PLIST_JOBS_KEY_LIGHTM] as? String // Light Magenta
            _jobss.append(jobs)
        }
    }
    return _jobss
}



func tableView(_ tableView: NSTableView, heightOfRow row: Int) -> CGFloat {
    return 80
}

func numberOfRows(in tableView: NSTableView) -> Int {
    return allJobss().count
}

func tableViewSelectionDidChange(_ notification: Notification) {
    print(tableView.selectedRow)

    let path = Bundle.main.path(forResource: "Jobs", ofType: "plist")
    let url = URL(fileURLWithPath: path!)
    let data = try! Data(contentsOf: url)
    let plist = try! PropertyListSerialization.propertyList(from: data, options: .mutableContainers, format: nil)

    let dictArray = plist as! [[String:String]]
    // [[String:String]] is equivalent to Array< Dictionary<String, String> >

    print(dictArray[tableView.selectedRow]["n"] as Any)



}



override var representedObject: Any? {
    didSet {
    // Update the view, if already loaded.
    }
}

}

NSTableview

Plist

希望有人能帮助我。

首先这是Swift。包含下划线字符的变量名是丑陋的 objective-c-ish.

你的工作流程是错误的。 objectValueFor 经常被调用(每行一次)并且应该 return 只是给定 row/column 的对象。一次又一次地读取相同的数据是可怕的。

viewDidLoad中加载数据一次,并直接将Plist解码为class。需要 class 才能绑定值

@objcMembers
class Job : NSObject, Decodable {
    dynamic var n, schwarz, cyan, magenta, gelb, lightc, lightm : String
}

@objc dynamic var jobs = [Job]()

override func viewDidLoad() {
    super.viewDidLoad()
    tableView.delegate = self
    tableView.dataSource = self

    // Do any additional setup after loading the view.
    let url = Bundle.main.url(forResource: "Jobs", withExtension: "plist")!
    let data = try! Data(contentsOf: url)
    jobs = try! PropertyListDecoder().decode([Job].self, from: data)
    tableView.reloadData()
}

objectValueFor 中只是 return 行的项目

func tableView(_ tableView: NSTableView, objectValueFor tableColumn: NSTableColumn?, row: Int) -> Any? {
     return jobs[row]
}

其他委托方法是

func numberOfRows(in tableView: NSTableView) -> Int {
    return jobs.count
}

func tableViewSelectionDidChange(_ notification: Notification) {
    let selectedRow = tableView.selectedRow
    if selectedRow == -1 {
       print("Nothing selected")
    } else {
      print("Row \(selectedRow) selected")
    }
}

您必须将值绑定到文本字段。打开 Interface Builder,⌃⇧-单击 table 视图中最左侧的文本字段(Table View Cell)上的 ,select TableView Cell,按⌥⌘7打开Bindings Inspector,点击下方的三角三角Value,勾选复选框 Bind to Table Cell View,在 Model Key Path 文本字段中写入 objectValue.n。将行中的其他文本字段绑定到相应的属性(objectValue.schwarz 等)。


可以删除

var n : String? = nil; // n
var schwarz : String? = nil; // Schwarz
var cyan : String? = nil; // Cyan
var magenta : String? = nil; // Magenta
var gelb : String? = nil; // Gelb
var lightc : String? = nil; // Light Cyan
var lightm : String? = nil; // Light Magenta

let AUTO_PLIST_JOBS_PATH = Bundle.main.path(forResource: "Jobs", ofType: "plist")

let AUTO_PLIST_JOBS_KEY_N = "n"   // n
let AUTO_PLIST_JOBS_KEY_SCHWARZ = "schwarz"   // Schwarz
let AUTO_PLIST_JOBS_KEY_CYAN = "cyan"   // Cyan
let AUTO_PLIST_JOBS_KEY_MAGENTA = "magenta"   // Magenta
let AUTO_PLIST_JOBS_KEY_GELB = "gelb"   // Gelb
let AUTO_PLIST_JOBS_KEY_LIGHTC = "lightc"   // Light Cyan
let AUTO_PLIST_JOBS_KEY_LIGHTM = "lightm"   // Light Magenta

func allJobss() -> [Jobs]{

    if _jobss.count > 0 {
        return _jobss
    }

    if let allDatas = NSArray(contentsOfFile: AUTO_PLIST_JOBS_PATH!) {

        for dict in allDatas {
            guard let dict = dict as? [String: AnyObject] else {continue}

            let jobs = Jobs()
            jobs.n = dict[AUTO_PLIST_JOBS_KEY_N] as? String // n
            jobs.schwarz = dict[AUTO_PLIST_JOBS_KEY_SCHWARZ] as? String // Schwarz
            jobs.cyan = dict[AUTO_PLIST_JOBS_KEY_CYAN] as? String // Cyan
            jobs.magenta = dict[AUTO_PLIST_JOBS_KEY_MAGENTA] as? String // Magenta
            jobs.gelb = dict[AUTO_PLIST_JOBS_KEY_GELB] as? String // Gelb
            jobs.lightc = dict[AUTO_PLIST_JOBS_KEY_LIGHTC] as? String // Light Cyan
            jobs.lightm = dict[AUTO_PLIST_JOBS_KEY_LIGHTM] as? String // Light Magenta
            _jobss.append(jobs)
        }
    }
    return _jobss
}