Re: NSArrayController Funkyness
Re: NSArrayController Funkyness
- Subject: Re: NSArrayController Funkyness
- From: Justin Anderson <email@hidden>
- Date: Fri, 16 Jan 2004 10:38:05 -0500
It sounds like you're doing more work than you need to. For the
functionality you're asking for, you don't need to subclass anything.
As long as the ordering of your NSArray is based on the key "name",
then why not just let the NSArrayController do the sorting work for
you? Bind your NSTableColumn's value to the NSArrayController's
arrangedObjects with a model key path of "name", and make two buttons
in IB that call add: and remove: respectively on the NSArrayController.
That does everything you've mentioned without a line of code. (Unless,
that is, you also wanted to maintain the option of returning to the
original unsorted state.)
I hope that's what you wanted,
Justin Anderson
On Jan 15, 2004, at 2:43 PM, email@hidden wrote:
I am using an NSArrayController to hold a list of objects that have a
"name" variable, which is used as the key to display them in a table.
The array of these object maintained by the NSArrayController object is
unordered, but I have subclassed NSArrayController to allow the user to
specify whether or not to have them ordered. If the user specifies that
the list should be displayed as ordered, then a flag is set and an
array created with the ordered objects. This ordered array is then
returned by arrangedObjects in place of the unordered array. This works
perfectly, as the proper list of names appears, though the table must
be told to reload its data. Doing it this way allows me to maintain the
content array as well as lazily update the sorted array.
The trouble comes when I try to remove objects from the table. I have a
button that has the array controller subclass as its target and the
remove: method as its action. When this button is pressed and the
unordered list (the actual content array) is displayed, this works just
fine. However, when the ordered list is displayed, the table thinks
that there is one more object in the list, leaving a blank space at the
bottom of the table and giving errors (when the blank space tries to
display itself). For instance, if there are 47 items displayed and the
last one is deleted, a blank space appears and we get:
*** -[NSCFArray objectAtIndex:]: index (46) beyond bounds (46)
*** malloc[842]: Deallocation of a pointer not malloced: 0xbfffe160;
This could be a double free(), or free() called with the middle of an
allocated block; Try setting environment variable MallocHelp to see
tools to help debug
Here is the code for removing objects:
//_contentSorted is the stored array of sorted objects.
//_useSorted is the flag.
//_sortingValid tells arrangedObjects to resort the array.
- (void)removeObjectAtArrangedObjectIndex:(unsigned int)index
{
id object;
if (_useSorted)
object = [_contentSorted objectAtIndex: index];
else
object = [[self content] content];
[self removeObject: object];
}
- (void)removeObject:(id)object
{
[super removeObject: object];
[self sortContent];
_sortingValid = NO;
}
- (void)removeObjects:(NSArray *)objects
{
[super removeObjects: objects];
[self sortContent];
_sortingValid = NO;
}
- (void)removeObjectsAtArrangedObjectIndexes:(NSIndexSet *)indexes
{
NSArray* content;
if (_useSorted)
content = _contentSorted;
else
content = [self content];
NSMutableArray* objects = [NSMutableArray array];
for (int index = [indexes firstIndex];
index != NSNotFound; index = [indexes indexGreaterThanIndex: index])
{
[objects addObject: [content objectAtIndex:index]];
}
[self removeObjects: objects];
}
_______________________________________________
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.
_______________________________________________
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.