Editing a TableView
Editing a TableView
- Subject: Editing a TableView
- From: "Gerriet M. Denkmann" <email@hidden>
- Date: Sat, 16 Oct 2010 15:00:51 +0700
I have got a window with an NSTableView which uses a data source.
The table has two colums labeled Key and Value.
It shows the keys and values of an NSMutableDictionary.
The data source method is:
- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
{
NSArray *allKeys = [ myDictionary allKeys ];
NSString *key = [ allKeys objectAtIndex: rowIndex ];
NSString *identifier = [ aTableColumn identifier ];
if ( [ identifier isEqualToString: kColumnKey ] )
{
return key;
}
else if ( [ identifier isEqualToString: kColumnValue ] )
{
NSString *value = [ myDictionary objectForKey: key ];
return value;
};
}
The documentation for "allKeys" states: "The order of the elements in the array is not defined."
Does this mean that subsequent method invocations to "allKeys" might get different arrays - even if the dictionary has not changed?
So that my table might contain random nonsense?
Probably not (never seen any problems here).
The table is editable. When the editing ends this method gets called:
- (void)controlTextDidEndEditing:(NSNotification *)aNotification
{
NSDictionary *userInfo = [ aNotification userInfo ];
NSTextView *tv = [ userInfo objectForKey: @"NSFieldEditor" ];
NSString *newEditValue = [ [ [ tv string ] copy ] autorelease ];
NSTableView * aTableView = [ aNotification object ];
NSInteger colE = [ aTableView editedColumn ];
NSArray *tableColumns = [ aTableView tableColumns ];
NSTableColumn *editedCol = [ tableColumns objectAtIndex: colE ];
NSString *identifier = [ editedCol identifier ];
if ( [ identifier isEqualToString: kColumnValue ] )
{
// now I need the key in order to do:
// [ myDictionary setObject: newEditValue forKey: key ];
// but [ aTableView editedRow ] is not reliable - see below
}
}
The window also has a button called "Add Key" which adds a key-value pair to the dictionary (and calls reloadData on the NSTableView).
When I have edited some value in some row of the Value column (but not hit CR yet) and then click the "Add Key" button then sometimes (not always) the value of "editedRow" does NOT give me the correct key for my edited value.
Depends either on whether the new key gets inserted above or below the key of the edited row (in the order of allKeys) or on some subtle timing issue.
Currently I use:
- (BOOL)tableView:(NSTableView *)aTableView shouldEditTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
{
NSString *identifier = [ aTableColumn identifier ];
if ( [ identifier isEqualToString: kColumnValue ] )
{
NSTableColumn *keyCol = [ aTableView tableColumnWithIdentifier: kColumnKey ];
self.keyOfValueBeingEdited = [ self tableView: aTableView objectValueForTableColumn: keyCol row: rowIndex ];
};
return YES;
}
But this seems kind of clumsy. I have the strong feeling that there must be a better way.
(I have experimented with NSControllers before - but there were some problems - so I decided to stick with the good old data source.)
10.6.4
Kind regards,
Gerriet.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden