2
votes

I have an app that contains an NSTableView instance with 2 columns. One column, is an instance of this:

#import <Cocoa/Cocoa.h>
@class NSTableView;
@interface MMCheckboxTableCellView : NSTableCellView
@property (weak) IBOutlet NSButton *checkboxField;
@end

and the implementation is all boilerplate with no custom code added at all. In code, the NSTableView instance sets tableview.enabled = YES (or NO) programmatically based on various state of the app. When tableview is disabled, the other column of the table, which is display only, stops responding to click actions, as expected. The checkbox column, above, continues to respond to clicks and lets the user check/uncheck the button instances.

To try to troubleshoot this, I added a Text Field (it's not hooked up to anything) in the the checkbox column and when the tableview is disabled, it isn't possible to enter text into the field. Is there something special about NSButton that I'm missing? Is it possible that because I derived this class from NSTableCellView with an added Check Box rather than using a Check Box Cell I'm running into this problem?

3

3 Answers

0
votes

I finally figured this out. It appears that even though the NSButton is embedded in the NSTableView, setting enabled=NO on the table doesn't control the behavior of the button. The only thing that eventually made this work was to invoke setEnabled the the control based on the state of the app. This had to happen in the rendering of the cell, so when the state changes, it was also necessary to invoke NSTableView:reloadData so the visible cells would redraw immediately.

0
votes

As an alternative, I decided to work around this same problem by setting up a computed property matching to my boolean property that returned either a blank or a checkmark character (✓). Then bound to this field as a normal NSTextfield for which the enabling/disabling works.

    // Swift 
    // Computed variables on model for readonly display in table
    var bIosText:String     {return bIos ? "✓" : " " }
    var bAndroidText:String {return bAndroid ? "✓" : " " }

Looks pretty good I think:

enter image description here

Of course this is only part of the solution, if you want to enable editing, you need to have multiple columns and hide/unhide as needed.

0
votes

Do not embed an NSButton in the table view, only use a NSButtonCell. If your NSTableView was view based, it will switch to cell based (at least Xcode did that for me).

So this is wrong:

view based, with NSButton

And this is right:

cell based

It should suffice to disable/enable the table view after this change, the button cell will behave accordingly.