Re: Removing an item from a NSTreeController
Re: Removing an item from a NSTreeController
- Subject: Re: Removing an item from a NSTreeController
- From: Felix Franz <email@hidden>
- Date: Tue, 3 Jul 2007 11:30:43 +0200
On Jul 3, 2007, at 5:09 AM, Ron Aldrich wrote:
Folks,
I'm struggling with a problem that I hope someone can shed some
light on.
I have a tree controller (DeviceTreeController) which maintains a
set of objects (Device). Device objects come and go at the whim of
the IORegistry.
When a device disappears from the IORegistry, I need to remove it
from the tree, but NSTreeController does not implement - (void)
removeObject: (id) object.
if you follow the usual KVO-guidelines for collections
you simply change your model-object (remove your devices) and
NSTreeController updates
the tree automatically. At least you have to define getters and
setters for the collection.
For example if your Device-class contains a array of "subdevices" you
have to
define -(NSArray*) subdevices and -(void) setSubdevices: (NSArray*) a
in your Device class.
Then you can use for example:
[[aDevice mutableArrayValueForKey: @"subdevices"]
removeObjectAtIndex: 2]
See <http://developer.apple.com/documentation/Cocoa/Conceptual/
CocoaBindings/Concepts/Troubleshooting.html>
(first item)
What NSTreeController *does* provide is -(void)
removeObjectAtArrangedObjectIndexPath: (NSIndexPath*) indexPath.
So, my problem is, how do I determine what the index path of a
given object is, so that I can remove it from the controller?
Mmmh, reading <http://developer.apple.com/documentation/Cocoa/
Conceptual/CocoaBindings/Concepts/CntrlContent.html#//apple_ref/doc/
uid/TP40002147-183285> says "Using these methods can provide a
performance increase over modifying the model objects directly and
relying on key-value observing.".
The simplest way should be to implement a indexPath-method in your
model-class
like:
- (NSIndexPath*) indexPath;
{
return [[[self container] indexPath] indexPathByAddingIndex:[[[self
container] orderedComponents] indexOfObject:self]];
}
(in this case container returns the parent). Your root-node simply
returns an empty index-path.
HTH
Cheers,
felix
_______________________________________________
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