• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Is there an easier way of changing NSButtonCell state in an NSOutlineView
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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: Quincey Morris <email@hidden>
  • Date: Wed, 23 Nov 2011 10:32:07 -0800

On Nov 23, 2011, at 07:58 , Gilles Celli wrote:

> I've setup an NSOutlineView with 2 colums: the first one named "buttonColumns" with NSButtonCell (Switch ON / OFF)
> and the second one with TextFieldCell…
>
> While the state change works for NSButtonCell in the buttonColum I had to create an NSArray *checks to set it ON or OFF.
> Is there an easier way to change the state of an NSButtonCell in an NSOutlineView without using an NSArray ?

NSOutlineView and NSTableView are interface elements that are backed by a data model. They do not store the data model, because they need to support (virtually) very large numbers of rows, and keeping the data model (or a copy of part of it) in the view would be too expensive of memory.

Therefore, you need to supply the data model, and then either provide a data source or bindings to connect it to the view. You need either (a) this array of BOOLs, or (b) a BOOL property of some objects in the rest of your data model.

> The code I'm using seems a little bit overkill for me...there must be a simpler solution…

For a data source, this amount of code isn't overkill. Data source methods often end up being wordy and ugly. You could eliminate the code altogether by using bindings, but you'd have to use solution (b) above -- bindings need an array property of some data model object, and your array doesn't qualify.

Also, I'm pretty sure your code is not doing what you thing it does:

> Here's what I've done so far and it works:
>
> // init code
>
> checks  = [[NSMutableArray alloc] init];
> for ( int i=0; i <  NBR_OF_ROWS;  i++ )
>        [checks addObject:[NSNumber numberWithInt:0]];
>
> // NSOutlineView datasource methods
>
> - (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
> {
>    id theValue;
>  NSInteger selectedRowIndex = [outlineView selectedRow];

You're getting the 'checks' value for the *selected* row, which is not the row whose object value is being asked for in this method. This means that when the selection changes, the check boxes on *all* of the rows will change (though you might not see that until redrawing of the entire view is forced).

Instead, you should convert the 'item' parameter to a row number.

>   if ( [[tableColumn identifier] isEqualToString:@"buttonColumn"] )
>   {
>      theValue = [checks objectAtIndex:selectedRowIndex];
>    }
>    return theValue;
> }
>
> - (void)outlineView:(NSOutlineView *)outlineView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn byItem:(id)item
> {
>    NSInteger selectedRowIndex = [outlineView selectedRow];
>
>    if ( [[tableColumn identifier] isEqualToString:@"buttonColumn" ])
>    {
>       [checks replaceObjectAtIndex:selectedRowIndex withObject:object];
>     }
> }

_______________________________________________

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

  • Follow-Ups:
    • Re: Is there an easier way of changing NSButtonCell state in an NSOutlineView
      • From: Gilles Celli <email@hidden>
References: 
 >Is there an easier way of changing NSButtonCell state in an NSOutlineView (From: Gilles Celli <email@hidden>)

  • Prev by Date: Re: Is there an easier way of changing NSButtonCell state in an NSOutlineView
  • Next by Date: Re: Querying system-wide base for file size calculation (base-2 vs base-10)
  • Previous by thread: Re: Is there an easier way of changing NSButtonCell state in an NSOutlineView
  • Next by thread: Re: Is there an easier way of changing NSButtonCell state in an NSOutlineView
  • Index(es):
    • Date
    • Thread