Preventing updates to NSTableView while editing
I have an NSTableView that has one column of editable fields. Cell editing works fine, and my delegate 开发者_运维百科routines get the update and can act on them as needed. The problem is that there is other code that updates values in the table based on timer or asynchronous (socket) input. When an updates event happens while editing is in progress, the update overwrites the user input.
I'm trying to use the delegate methods to block updates with an instance variable lock:
- (BOOL)control:(NSControl *)control textShouldBeginEditing:(NSText *)fieldEditor;
{
tableEditInProgress = YES;
return YES;
}
- (BOOL)control:(NSControl *)control textShouldEndEditing:(NSText *)fieldEditor
{
tableEditInProgress = NO;
return YES;
}
- (void)controlTextDidBeginEditing:(NSNotification *)aNotification
{
tableEditInProgress = YES;
}
- (void)controlTextDidEndEditing:(NSNotification *)aNotification
{
tableEditInProgress = NO;
}
This only seems to work if the user actually types new text in the field before the update happens. I want updates to be blocked as soon as the user gets an edit cursor in the field (double click field contents).
I'm probably just using the wrong delegate methods.
TIA
joe
Try ditching all this stuff, and try this instead:
When you want to check whether the table is currently being edited, call [tableView currentEditor];
If this is non-nil, the table view is being edited. If it's nil, it's not being edited. That is:
BOOL tableEditInProgress = ([tableView currentEditor] != nil);
精彩评论