1.代码创建的NSTableView的代理方法不执行
原因是代码创建的tableView默认没有column,需要创建column
func initializeSubView(superViewFrame frameRect: NSRect) -> Void
{
pmdScrollView = NSScrollView.init(frame: frameRect)
pmdTableView = NSTableView.init(frame: frameRect)
let column = NSTableColumn.init(identifier: NSUserInterfaceItemIdentifier(rawValue: "columnFirst"))
column.width = frameRect.size.width
pmdTableView.addTableColumn(column)
pmdScrollView.documentView = pmdTableView
pmdTableView.delegate = self
pmdTableView.dataSource = self
self.addSubview(pmdScrollView)
}
代理方法
func numberOfRows(in tableView: NSTableView) -> Int {
return 2
}
/* Optional - Different cells for each row
A different data cell can be returned for any particular tableColumn and row, or a cell that will be used for the entire row (a full width cell). The returned cell should properly implement copyWithZone:, since the cell may be copied by NSTableView. If the tableColumn is non-nil, and nil is returned, then the table will use the default cell from [tableColumn dataCellForRow:row].
When each row is being drawn, this method will first be called with a nil tableColumn. At this time, you can return a cell that will be used to draw the entire row, acting like a group. If you do return a cell for the 'nil' tableColumn, be prepared to have the other corresponding datasource and delegate methods to be called with a 'nil' tableColumn value. If don't return a cell, the method will be called once for each tableColumn in the tableView, as usual.
*/
/*
该代理方法是基于cell-base的,如果实现了View-Base 的代理方法,TableView 则是基于 view-based, 不会再触发该基于 cell-base
的代理方法
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView?
*/
func tableView(_ tableView: NSTableView, dataCellFor tableColumn: NSTableColumn?, row: Int) -> NSCell? {
return nil
}
/* View Based TableView:
Non-bindings: This method is required if you wish to turn on the use of NSViews instead of NSCells. The implementation of this method will usually call -[tableView makeViewWithIdentifier:[tableColumn identifier] owner:self] in order to reuse a previous view, or automatically unarchive an associated prototype view for that identifier. The -frame of the returned view is not important, and it will be automatically set by the table. 'tableColumn' will be nil if the row is a group row. Returning nil is acceptable, and a view will not be shown at that location. The view's properties should be properly set up before returning the result.
Bindings: This method is optional if at least one identifier has been associated with the TableView at design time. If this method is not implemented, the table will automatically call -[self makeViewWithIdentifier:[tableColumn identifier] owner:[tableView delegate]] to attempt to reuse a previous view, or automatically unarchive an associated prototype view. If the method is implemented, the developer can setup properties that aren't using bindings.
The autoresizingMask of the returned view will automatically be set to NSViewHeightSizable to resize properly on row height changes.
*/
/*
该代理方法是基于view-base的,如果实现了该方法,tableView 就是 view-base 的, 就不会再触发 cell-base 的方法
func tableView(_ tableView: NSTableView, dataCellFor tableColumn: NSTableColumn?, row: Int) -> NSCell?
*/
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
return colorEQItem
}
/* This method is required for the "Cell Based" TableView, and is optional for the "View Based" TableView. If implemented in the latter case, the value will be set to the view at a given row/column if the view responds to -setObjectValue: (such as NSControl and NSTableCellView). Note that NSTableCellView does not actually display the objectValue, and its value is to be used for bindings. See NSTableCellView.h for more information.
*/
/*
该代理方法是基于 cell-base 才会触发的,
*/
func tableView(_ tableView: NSTableView, objectValueFor tableColumn: NSTableColumn?, row: Int) -> Any? {
return nil
}
网友评论