Re: Is there an easier way of changing NSButtonCell state in an NSOutlineView
Re: Is there an easier way of changing NSButtonCell state in an NSOutlineView
- Subject: Re: Is there an easier way of changing NSButtonCell state in an NSOutlineView
- From: Gilles Celli <email@hidden>
- Date: Fri, 25 Nov 2011 16:37:32 +0100
[SOLVED] :-)
I've succesfully figured out how to add and change the NSButtonCell state in the NSOutlineView. I just added a BOOL isChecked to my outlineview Item class
so that I don't need an NSArray, which is much cleaner and I don't need the rowIndex: [outlineView rowForItem:item].
Here's my partial code for reference:
OvItem class:
@interface OvItem : NSObject {
NSString *name;
NSMutableArray *children;
BOOL isChecked;
}
@property (nonatomic, readwrite, retain) NSString *name;
@property (nonatomic, readwrite, retain) NSMutableArray *children;
@property (nonatomic, readwrite, assign) BOOL isChecked;
Then for the outlineView datasource methods:
- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item {
OvItem *node = ((OvItem *)item == nil ? rootNode : (OvItem *)item);
return [node.children objectAtIndex:index];
}
- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
{
id theValue;
if ( [[tableColumn identifier] isEqualToString:@"instrumentColumn"] )
{
theValue = [ (OvItem *)item name];
}
else if ( [[tableColumn identifier] isEqualToString:@"buttonColumn"] )
{
// Reading the NSButtonCell state value
// Since NSButtonCell's state is an intValue we convert it to an NSNumber
theValue = [NSNumber numberWithInt:[(OvItem *)item isChecked]];
}
return theValue;
}
- (void)outlineView:(NSOutlineView *)outlineView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
{
// Setting NSButtonCell state in outlineView's buttonColum
if ( [[tableColumn identifier] isEqualToString:@"buttonColumn" ])
{
if ( [(OvItem *)item isChecked] )
{
[(OvItem *)item setIsChecked:FALSE];
}
else {
[(OvItem *)item setIsChecked:TRUE];
}
// NSLog(@"item name: %@ | item isChecked: %d ", [(OvItem *)item name], [(OvItem *)item isChecked] );
}
}
Cheers,
Gilles
On Nov 24, 2011, at 6:47 PM, Quincey Morris wrote:
> On Nov 24, 2011, at 05:46 , Gilles Celli wrote:
>
>> My hierearchical list will not changed or sorted in the NSoutlineView….that's why I was thinking to use NSArray for the buttons.
>
> In your scenario -- where the list is "really" flat but displayed hierarchically for esthetic reasons -- the array approach is fine.
>
>> However if I want to do it correctly, should I add an ivar like BOOL isChecked to my OvItem class ? So I won't need an NSArray
>>
>> Like this:
>>
>> @interface OvItem : NSObject {
>>
>> NSString *name;
>> NSMutableArray *children;
>> BOOL isGroup;
>> BOOL isChecked; // For NSButtonCell in the NSOutlineView
>> }
>>
>> Do you think this is a better idea than using NSArray ?
>
> If you're *already* using these OvItem objects, then I'd say it's worth doing this. It makes things a bit clearer when you (or someone else) return to the code in the future. It would probably also allow you to simplify your 'outlineView:objectValueForTableColumn:byItem:' method a bit.
>
>
_______________________________________________
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