views:

171

answers:

2

Can anyone guide me as to how to create a table using Objective C for Mac OS X. The program should read values from an NSArray object and display the values in the table. I would appreciate it if anyone helped me out.

+1  A: 

You need to implement a class that responds to NSTableDataSource and instantiate this, attaching it to your table as the datasource.

A quick search of NSTableDataSource (or indeed NSTableView [or for that matter Table]) in the Developer Docs should get you the details you need.

Edit:

You'll need to implement a couple of methods on your datasource as appointed to the tableview:

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

- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex {
  return [[myArray objectAtIndex:rowIndex] objectForKey:[aTableColumn identifier]];
}

Together these two should just about do exactly what you need.

Williham Totland
Hey iam a newbie to obj-c ,iam just out of collg and i have joined a company 3 weeks before and i started obj-c just two weeks before so plz bear with me ..My task was to parse an xml file and display the attributes and its values onto a table... I have coded the backend i.e i parsed the xml file and stored each profile in an NSMutableDictionary obj and finally created an NSMutableArray and stored the dictionary objects into it..so this is working fine..but my issue is with the frontend...i dont kno how to create the table and display the contents of the array in it..so plz help me out.
i wld appreciate if anyone writes the code for me..
@lohith: There you go. In addition you need to assign whatever object these are implemented on (your controller object, probably) to the tableview as its datasource (you do this in Interface Builder).
Williham Totland
A: 

In answer to another question ( http://stackoverflow.com/questions/2432459/how-to-add-a-scrollable-nstableview-programmatically/2438863#2438863 ), I created the following sample application:

http://ericgorr.net/cocoadev/TableViewInCode.zip

which will demonstrate what you need to know.

Being new to all of this, I would highly recommend:

(1) Picking up a copy of "Cocoa Programming for Mac OS X (Third Edition)" by Aaron Hillegass

(2) Picking up a copy of "Cocoa Design Patterns" by Erik Buck and Donald Yacktman

I would consider these to be two must read books for anyone just starting out.

ericgorr