NSArrayController and
NSArrayController and
- Subject: NSArrayController and
- From: Erik Stein <email@hidden>
- Date: Mon, 8 Mar 2004 10:46:05 +0100
Hello all --
I'm digging deep into google, my mail-archivs and evil class-dump hacks
just to find an optimal NSArrayController implementation which supports
drag & drop, copy & paste, undo and item reordering.
Its difficult to understand if my following question already got
answered, but then the recent NSEditor thread showed that the continual
reiteration of questions concerning Cocoa Bindings is rather helpful
for all. Thanks to all and especially the apple guys for their
patience.
My model only has a title and content. No problems with these. Now I
like to have a column in the table view that displays the index of the
model object inside the content array (for that reordering of items
makes sense and is visible in the user interface).
As a workaround I established a link between the array controller and
each model object:
@implementation MyArrayController
- (NSArray *)arrangeObjects:(NSArray *)objects
{
{ // Workaround to get cards respond with their index in the document
NSEnumerator *enumator = [objects objectEnumerator];
MyCard *obj;
while (obj = [enumator nextObject]) {
[obj setArrayController:self];
}
}
...
}
- (unsigned int)contentIndexForObject:(id)object
{
return [[self content] indexOfObject:object];
}
...
and in MyCard.m is implemented
@implementation MyCard
- (unsigned int)indexInDocument
{
if (arrayController != nil) {
unsigned int index = [arrayController contentIndexForObject:self];
return (index + 1);
}
else return 0;
}
...
The NSTableColumns "value" binding is bound to the
"arrangedObjects.indexInDocument" key. This approach is working but
it's IMHO not very "good" in the sense of a logical objects interplay.
My first idea was to override MyArrayController's valueForKeyPath:
method, expecting that request for the key
"arrangedObjects.indexInDocument" are routed through it. It's not, and
obviously cannot since there is no place in a keypath to ask for a
specific index (like "arrangedObjects.title.forObjectAtIndex13" :-)
My next idea was to bind the indexInDocument table column to a custom
MyArrayControllers "arrangedObjectsIndexes.intValue" keypath:
@implementation MyArrayController
- (NSArray *)arrangedObjectsIndexes
{
NSEnumerator *enumator = [[self arrangedObjects] objectEnumerator];
id obj;
NSMutableArray *returnValue = [NSMutableArray arrayWithCapacity:[[self
arrangedObjects] count]];
while (obj = [enumator nextObject]) {
[returnValue addObject:[NSNumber numberWithInt:[self
contentIndexForObject:obj]]];
}
return [NSArray arrayWithArray:returnValue];
}
...
This does not work because the table cells now display the whole array
instead of the number at the row's index. It's not clear to me why,
this method has a return value similar to
NSArrayController
- (NSArray *)arrangeObjects:(NSArray *)objects
- (id)arrangedObjects
It seems to me that the M<->C<->V scheme in a NSTableView (~Column),
NSArrayController, ModelObject setup has several layers where only the
array manipulation aspect is public i.e. adaptable by mortals. My
present understanding is that the bindings between table rows/cells and
the model properties are routed through private NSBinders and
subclasses.
Can you imagine a way to customize the table view row/cell bindings in
a Cocoa Bindings setup?
Thanks for your help
-- erik
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.