NSArrayController Funkyness
NSArrayController Funkyness
- Subject: NSArrayController Funkyness
- From: email@hidden
- Date: Thu, 15 Jan 2004 14:43:09 -0500
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.