MLowijs
MLowijs

Reputation: 1495

Custom NSView in NSTableView not showing all subviews

I'm trying to create a custom NSView to display in a column in a view-based NSTableView. The view contains 2 subviews, an NSTextField and an NSButton. I want the button to stay the width set by the constraints, and the textfield to resize when the NSView is resized. Below is a small animation showing the NSView and its subviews, and the constraints I created.

Creating constraints

As you can see, resizing the NSView works as expected.

Now, when displaying this custom NSView in an NSTableView, it looks as if the button just disappears, and resizing the column makes the textfield resize with it (the 'Category' column).

Button is gone

The coded used to create the NSView in tableView:viewForTableColumn:row:

let identifier = tableColumn!.identifier

if identifier == "Category" {
    var view = tableView.makeViewWithIdentifier(identifier, owner: self) as? TableCategoryView

    if view == nil {
        view = TableCategoryView(frame: tableView.frame)
        view!.identifier = identifier
    }

    return view
 }

The strange thing is, when there are no constraints on the 2 views, the button and textfield are both happily displayed inside the column, but they then of course don't resize with the table column width.

What am I doing wrong?

EDIT: It looks like something else is wrong. The NSView itself isn't resizing at all with the table column.

Upvotes: 0

Views: 776

Answers (1)

Heinrich Giesen
Heinrich Giesen

Reputation: 521

I think TableCategoryView is your new class and it is subclassed from NSView and shall replace the NSTableCellView you get when creating (in IB) a view based NSTableView. If you really want to create your own TableCellView it should be a direct subclass of NSTableCellView not NSView.

But in your case (add a button to the TableCellView) you do not need to create a new class. The existing TableCellView object already has a TextField (a property) with the name textField. Then simply drag (means: add) a button into the existing TableCellView (resize it and set the constraints) and drag a link from the button to a corresponding method in the delegate of the TableView. In the "corresponding method" you can ask for the clicked row and column and identify the click button. I did so for a TableView and for me it works well.

Upvotes: 1

Related Questions