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.
views:
171answers:
2You 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.
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.