2
votes

Is there any way to merge cell's in a NSTableView?

I know there are two modes for displaying data in a NSTableView: cell-based and view-based. If the data are presented by cells, the function

tableView(_ tableView: NSTableView, viewFor tableColumn:  NSTableColumn?, row: Int) 

is responsable, otherwise

tableView(_ tableView: NSTableView, rowViewForRow row: Int)

Currently I'm using the first one. In iOS, you can use the cell prototype in interface builder to link the views with a custom UITableViewCell, but how can I achieve this for a complete row in a NSTableView?

1
No, tableView(_:viewFor:row:) is a view based table view method. "This method is required if you want to use NSView objects instead of NSCell objects for the cells within a table view."Willeke
I think you might be confusing "cells" with an individual square in a table. A cell, in Cocoa-speak, is an object that performs the rendering of a view (typically, a control). For example, an NSButton has a NSButtonCell object that actually does the drawing. If you want to "merge" the cells of a row and use a single view to draw the entire row, you need to implement - [NSTableViewDelegate tableView:rowViewForRow:]. That method gets called first, and if it returns a view, that view is used for the entire row.James Bucanek
Thinking about it, I'm not sure that's correct. But there is a way for table delegate to specify which rows show individual views and which ones are "whole" row views. I'd start with the WWDC view-based table view videos and check out some of the sample projects—since neither the Table View Programming Guide or API documentation seem to explain this.James Bucanek

1 Answers

1
votes

Maybe I found a solution: You can use both

tableView(_:viewFor:row:)

and

tableView(:viewFor:tableColumn:row:)

but you have to be aware that the tableView is not using both methods for one cell (mutually exclusive).

Now you can define a xib, with a single UIView in it. Change the class of the UIView to NSTableRowView. Next, load the class in viewDidLoad into your table:

if let nib: NSNib = NSNib(nibNamed: NSNib.Name("SpecialRow"), bundle: nil) {
    self.tableView?.register(nib, forIdentifier: NSUserInterfaceItemIdentifier("specialRow"));
}

In your table method rowViewForRow you can do the following:

if let srow: SpecialRow = tableView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "specialRow"), owner: nil) as? SpecialRow {
    // ...

    return srow;
}