准备
首先,你的ViewController必须从NSTableViewDelegate
和NSTableViewDataSource
继承:
class ViewController: NSViewController, NSTableViewDelegate, NSTableViewDataSource
然后,像iOS中必须实现的NSTableView
委托方法。
做一个IBOutlet
,是这样的:
@IBOutlet weak var tableView: NSTableView!
代表和DataSource
然后在viewDidLoad中(),设置委托和数据源,以自我:
tableView.setDelegate(self)
tableView.setDataSource(self)
而实施所需方法:
对于行数:
func tableViewSelectionDidChange(notification: NSNotification) {
var row = tableView.selectedRow
// a row was selected, to something with that information!
}
如果您有任何(可选):
func numberOfRowsInTableView(tableView: NSTableView) -> Int {
return yourData.count //your data ist the array of data for each row
}
对于行数据:
func tableView(tableView: NSTableView, objectValueForTableColumn tableColumn: NSTableColumn?, row: Int) -> AnyObject? {
if tableColumn!.title == "firstColumnTitle" //if you have more columns
{
return yourData[row].first
}
else //second column
{
return return yourData[row].second
}
}
供选择问题,请不要犹豫!
你了解Obj-C吗? –