我一直试图在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.
}
}
}
我希望有人能帮助我。
答案 0 :(得分:0)
首先,这是Swift。包括下划线字符在内的变量名称很难看,并且 objective-c-ish 。
您的工作流程有误。 objectValueFor
经常被调用(每行一次),并且应该只返回给定行/列的对象。一次又一次地读取相同的数据是可怕的。
一次加载数据到viewDidLoad
中,并将Plist直接解码为一个类。需要一个类来绑定值
@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
中,仅返回该行的项目
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 View 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
}