Reputation: 23134
Here is the code:
import Cocoa
class ViewController: NSViewController, NSTableViewDataSource, NSTableViewDelegate {
let data = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[1, 2, 3, 4],
[5, 6, 7, 8],
]
@IBOutlet weak var tbl: NSTableView!
override func viewDidLoad() {
print("init")
super.viewDidLoad()
tbl.delegate = self
tbl.dataSource = self
tbl.target = self
tbl.reloadData()
print("end init")
}
override var representedObject: Any? {
didSet {
tbl.reloadData()
}
}
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
if let cell = tbl.make(withIdentifier: tableColumn!.identifier, owner: nil) as? NSTableCellView {
if let col = Int(tableColumn!.identifier) {
print("col id: ", tableColumn!.identifier)
print(row, col)
cell.textField?.stringValue = String(data[row][col])
return cell
}
}
return nil
}
func tableView(_ tableView: NSTableView, objectValueFor tableColumn: NSTableColumn?, row: Int) -> Any? {
if let col = tableColumn {
if let colIndex = Int(col.identifier) {
print(data[row][colIndex])
return data[row][colIndex] as Any
}
}
return nil
}
func numberOfRows(in tableView: NSTableView) -> Int {
return data.count
}
}
I am just trying to load a matrix into the table view, but it is blank when the app runs. What went wrong here?
Judging from the debug info, the functions tableView
were never invoked.
Upvotes: 0
Views: 212
Reputation: 56
I see you set the delegate of the tableView, but I don't see where you set the data source of the tableView.
Upvotes: 2