 |
 |
Starting with Cocoa
|
 |
|
 |
|
Forum Regular
Join Date: Sep 2002
Location: The land of evil: Redmond
Status:
Offline
|
|
I am trying to write my first real objective-c application. I am having trouble figuring out how to load data at the start of my program to fill the NSTableView that I have. I am new to Cocoa so I don't really understand how it all works. I understand that subclassing NSApplication is not a good idea. Do I use some kind of delegate (which I don't really understand)? I've tried finding tutorials and am not finding what I need.
|
|
12" PB 867 *Retired :( *
2.2 Ghz 15" Macbook Pro
|
| |
|
|
|
 |
|
 |
|
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status:
Offline
|
|
You shouldn't need to load data at the very start of your program, necessarily. I'd just load the data the first time it's needed by the tableview or when the datasource is initialized — unless it's a particularly expensive read operation and you feel the need to make it a special case.
If you do want to do something at the start of your program, you can use the applicationDidFinishLaunching: method of NSApp's delegate or an NSApplicationDidFinishLaunchingNotification.
For more about delegation and notifications, there's a good StepWise article on the topic.
|
|
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Sep 2002
Location: The land of evil: Redmond
Status:
Offline
|
|
Originally posted by Chuckit:
You shouldn't need to load data at the very start of your program, necessarily. I'd just load the data the first time it's needed by the tableview or when the datasource is initialized — unless it's a particularly expensive read operation and you feel the need to make it a special case.
If you do want to do something at the start of your program, you can use the applicationDidFinishLaunching: method of NSApp's delegate or an NSApplicationDidFinishLaunchingNotification.
For more about delegation and notifications, there's a good StepWise article on the topic.
I need to load the data into a list/table that opens up at the very beginning of the program. How would I accomplish something like this. I am used to being able to do these kinds of things in java and with interface builder I am completely clueless as to how to do these things in objective-c. What I'm looking for is something that reads in a file of data and fills the table that is opened at the start of the application.
I've read that page before and am still clueless as to how to actually use and create delegates.
Edit: What is this Notification thing? Is it like a Listener in java? If so how would I use it?
|
|
12" PB 867 *Retired :( *
2.2 Ghz 15" Macbook Pro
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Nov 2001
Status:
Offline
|
|
You probably don't need to set the NSApp delegate. A delegate is an arbitrary object that is sent messages by another object, like -applicationDidFinishLaunching: which is sent by the shared NSApplication instance to its delegate instance variable. In IB, create a new subclass of NSObject named something like TableController. If this is your MainMenu.nib file (which I'm assuming it is), you should instantiate this class -- if it were any other nib file you would generally set this object as the File Owner. In IB, draw a connection between the table view and the controller, and set it as the table view's dataSource outlet. Then choose Create Files for TableController.
In TableController.h add an ivar for your data. I'll assume it's a plist, but it can be anything you can parse.
Code:
@interface TableController : NSObject {
NSArray *tableRows;
}
@end
TableController.m should look something like:
Code:
- (void)dealloc {
[tableRows release]; // always clean up memory
[super dealloc];
}
- (NSArray *)tableRows {
// This is a delayed read, which is good for performance
if (!tableRows) {
tableRows = [[NSArray alloc] initWithContentsOfFile:@"path/to/rows"];
}
return tableRows;
}
- (int)numberOfRowsInTableView:(NSTableView *)aTableView {
return [[self tableRows] count];
}
- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:
(NSTableColumn *)aTableColumn row:(int)rowIndex {
return [[self tableRows] objectAtIndex:rowIndex];
// here we ignore the table column, but you could implement
// tableRows as an array of dictionary objects, and return
// [[[self tableRows] objectAtIndex:rowIndex]
// objectForKey:[aTableColumn identifier]]
// if you want a multi-column table
}
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Sep 2002
Location: The land of evil: Redmond
Status:
Offline
|
|
Thank you! This was exactly what I was needing. I also found a good example program in /Developer/Examples/AppKit/ClockControl. Now I just have to figure out plists and NSDictionaries and I think I will be set for this program. FYI I'm programming a small helper program for Civilization the board game by Avalon Hill. I highly recommend this game to strategy game lovers.
|
|
12" PB 867 *Retired :( *
2.2 Ghz 15" Macbook Pro
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |