我对Swift编程还很陌生,(从来没有Objective C)。当我点击当前tableview中的一个项目时,我正在尝试添加到NSTableView。这些项目似乎在点击时被添加,但是表格似乎并没有用数组中的新东西来刷新。为什么NSTableView不能重新加载?
我已经在过去几天尝试了各种各样的东西,让它在主线程和UI线程上运行reloadData等,但我感觉我只是碰壁(它肯定不能是这个很难做到这样简单的事情,就像我可以在几分钟内在Java)...
我有遗漏的东西很明显吗? (下面的代码)
class TableViewController: NSTableView, NSTableViewDataSource, NSTableViewDelegate {
var items: [String] = ["Eggs", "Milk"]
func tableView(tableView: NSTableView, viewForTableColumn tableColumn: NSTableColumn?, row: Int) -> NSView? {
let result : TableCell = tableView.makeViewWithIdentifier(tableColumn!.identifier, owner: self) as! TableCell
result.itemField.stringValue = items[row]
return result
}
func numberOfRowsInTableView(tableView: NSTableView) -> Int {
return items.count
}
func tableViewSelectionDidChange(notification: NSNotification) {
let index = notification.object!.selectedRow
if (index != -1) {
NSLog("@%d", index)
items.append("Blah")
NSLog("@%d", items.count)
dispatch_async(dispatch_get_main_queue(), {() -> Void in
self.reloadData()
})
}
}
func relloadData() { //relload is not a typo
self.reloadData()
}
}
你的班级**是** tableview的故意吗?和'relloadData'的目的是什么? – luk2302
是的(这就是我在教程中一直遵循的方式,除非它不是我不知道的最佳实践)。重新数据的目的是用于调试目的。 – user3601148
主线程和UI线程是两个线程吗?这些项目是否添加? – Willeke