1

I have some editable cells in an NSTableView.

I enter a cell by double clicking on it. I have other buttons on the window. Any time I click on a button, this terminates the cell editing in a way I just get nothing in the cell and the NSTableView is not refreshed, even if I call [myTableView reloadData].

A possible workaround would be to disable all the buttons as soon as I start a cell edit in the NSTableView but I do not know how to detect this.

I can capture things as soon as something is typed in with :

(void)controlTextDidBeginEditing:(NSNotification *)notification

But this is not what I want, it's just too late, because it only gets called after the user types into the cell editor.

I want to detect exactly when the cell editing starts, even if nothing has been typed yet.

I have seen some posts with a possible outcome:

It’s best to first ask the window to become firstResponder (thus taking firstReponder away from the focused field), and if that failed, to resort to the crude “endEditingFor:” method on NSWindow.

But this is just greek to me. Any help is much appreciated.

Thomas Tempelmann
  • 11,045
  • 8
  • 74
  • 149
fredzouille
  • 137
  • 5
  • Is the table view cell based or view based? – Monolo Apr 17 '14 at 17:15
  • I, too, have this problem, i.e. detecting when editing starts. Solutions I found are all not working (using 10.11 SDK, building for 10.6): Neither the suggestions in http://www.cocoabuilder.com/archive/cocoa/142689-nstableview-editing.html nor the TN (https://developer.apple.com/library/content/qa/qa1551/_index.html). They either come too early, when the editor is not active yet (`shouldEditTableColumn`), too late (TN) or never get called (`editWithFrame` in `NSTextFieldCell` subclass). – Thomas Tempelmann Mar 20 '17 at 10:37

2 Answers2

1

The solution to detect when a cell's field editor starts, waiting for input, is to override the cell's setUpFieldEditorAttributes: method (and then invoke its super's message), like this:

-(NSText *)setUpFieldEditorAttributes:(NSTextView *)textObj
{
    NSTableView *table = (NSTableView*) self.controlView;
    NSLog(@"editing starts in row %d col %d", (int)table.editedRow, (int)table.editedColumn);
    return [super setUpFieldEditorAttributes:textObj];
}
Thomas Tempelmann
  • 11,045
  • 8
  • 74
  • 149
-1

Finally I did this and this did help :

The common workaround is to make sure the NSTextFieldCell is not the firstResponder anymore. In each button triggered ACTION method, just add this :

 program.h
 IBOutlet NSDatePicker        *dateDebut;

 program.m
 - (IBAction)currYear:(id)sender
 {
    [[dateDebut window] makeFirstResponder:dateDebut]; // or any other IBOutlet in the NSWindow
    ...
 }

This forces the end of the cell edition so that we get something clean.

fredzouille
  • 137
  • 5
  • While this seems to be a work-around for the specific setup of the original author of the question, this is not a generally useful answer to the question asked in the title. – Thomas Tempelmann Mar 20 '17 at 10:47