Talk About Network

Google





Mac > Mac Programmer Help > Re: Sample code...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 4 of 4 Topic 3767 of 3889
Post > Topic >>

Re: Sample code/tutorial(s) that deal with NSTableView?

by Gregory Weston <uce@[EMAIL PROTECTED] > Jun 10, 2008 at 02:09 PM

In article <K5mdnV53a5dRANPVnZ2dnUVZ_hCdnZ2d@[EMAIL PROTECTED]
>,
 Don Bruder <dakidd@[EMAIL PROTECTED]
> wrote:

> After going through the docs I can find on the NSTableView class, I'm 
> still left with no clue how to work with the darn things. They look like

> they'd be the ideal way to present the data in the project I'm messing 
> with to the user (I gather my data and stuff it into an NSMutableArray 
> holding "items" - Each individual item is an object with all of the 
> pertinent data for that item - Description, ID number, price, etc) but I

> can't figure out how to even get started using the class. 
> 
> The NSTableView reference is fine - as far as it goes. Ditto the 
> "conceptual" that goes with it. But neither helps me at all in actually 
> *USING* them!
> 
> I need something that shows me what's involved in actually making it 
> work.

It's actually fairly simple. You have to identify to the table view an 
object that will be the table's data source - you can do that in IB or 
in code. You also want to assign (in IB) useful strings to the 
'identifier' value for each column. If your data objects were 
dictionaries or are KVC-compliant, the names of the keys you use would 
be excellent choices.

The data source object needs to implement two methods (3 if your table 
accepts input).


- (int)numberOfRowsInTableView:(NSTableView*)aTableView
{
  return [myArray count];
}

- (id)tableView:(NSTableView *)aTableView 
objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex
{
  id theIdentifier = [aTableColumn identifier];
  id theEntry = [myArray objectAtIndex:rowIndex];
  id theResult = nil;
  /*
  ** Some code that uses theIdentifier to extract the proper value
  ** from theEntry and assign it to theResult. If your entries are
  ** dictionaries or KVC-compliant, for instance:
  **   theResult = [theEntry objectForKey:theIdentifier];
  */
  return theResult;
}

If any table columns are editable:

- (void)tableView:(NSTableView *)aTableView setObjectValue:(id)anObject 
forTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex

{
  id theIdentifier = [aTableColumn identifier];
  id theEntry = [myArray objectAtIndex:rowIndex];
  /*
  ** Code that modifies theEntry appropriately, such as:
  **   [theEntry setObject:anObject forKey:theIdentifier
  */
}

Those are all that are required. There are some additional methods you 
can implement if you want to handle sorting and/or dragging. You can 
also assign an object as the table's delegate; that gets to handle other 
messages that do things like re****t state changes in the table as a 
whole.

-- 
"Harry?" Ron's voice was a mere whisper. "Do you smell something ...
burning?"
   - Harry Potter and the Odor of the Phoenix
 




 4 Posts in Topic:
Sample code/tutorial(s) that deal with NSTableView?
Don Bruder <dakidd@[EM  2008-06-10 08:29:15 
Re: Sample code/tutorial(s) that deal with NSTableView?
Reinder Verlinde <rein  2008-06-10 19:57:10 
Re: Sample code/tutorial(s) that deal with NSTableView?
Don Bruder <dakidd@[EM  2008-06-10 11:40:20 
Re: Sample code/tutorial(s) that deal with NSTableView?
Gregory Weston <uce@[E  2008-06-10 14:09:39 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
localhost-V2008-12-19 Wed Jan 7 19:21:45 PST 2009.