Alright, so I'm learning programming in Objective-C and Cocoa, and I've run into a problem in the project I'm working on. So in my code, I have a NSMutableArray (sources) of objects of a custom class. Each object has a NSString name. I'm trying to get a table view to display all the objects in sources, by the name of each object.
I have the app delegate class following the NSTableViewDataSource protocol, which means having an objectValueForTableColumn method, and a numberOfRowsInTableView method.
In numberOfRowsInTableView, I just have a return [sources count]; statement, and it works fine.
In objectValueForTableColumn, I have return [[sources objectAtIndex: rowIndex] name];, but nothing is displayed.
I have added a lot of NSLog statements for debugging, and I think I know what the problem is, but I don't know why, or how to fix it.
So, inside objectValueForTableColumn, the sources array is nil. Inside, numberOfRowsInTableView, sources is active, and that method works fine. Now, both these methods run many times, and every time, sources is active in numberOfRowsInTableView, but nil in objectValueForTableColumn.
I replaced the return statement in objectValueForTableColumn with return @"Test string.";, and it displays the correct number of rows in the table view. Because of this, I know that the table view is set up correctly, besides the problem I am having with sources.
I can't figure out why sources works in one method, but not the other. I also haven't found a solution to get my table view working correctly. Any help or insight would be appreciated.
Thanks, Alex
edit:
- (void) loadFromFile
{
NSString *filePath = @"/Users/alex/opt/Wallify.plist";
NSFileManager *filemgr = [NSFileManager defaultManager];
if ([filemgr fileExistsAtPath: filePath])
{
NSData *data = [[NSData new] initWithContentsOfFile: filePath];
NSLog(@"Loaded data from file.");
self.sources = [NSKeyedUnarchiver unarchiveObjectWithData: data];
[data release];
for ( ATImageSource *i in sources)
{
NSLog(@"retaining i");
[i retain];
}
[sources retain];
NSLog(@"&imgsrc: %p", sources);
NSLog(@"imgsrc: %@", sources);
NSLog(@"Name: %@",[[sources objectAtIndex: 0] name]);
}
else
{
// Saved settings file does not exist
// Load defaults
self.sources = [[NSMutableArray alloc] initWithArray: nil];
[self.sources retain];
// new code to setup testfoldersrc
ATFolderSource *f = [ATFolderSource new];
f.name = @"Documents";
f.source = @"/Users/alex/Documents/";
f.localPath = @"/Users/alex/Documents/";
f.type = @"f";
[f populateArrayFromLocalPath];
[self.sources addObject: f];
[f release];
// new code to setup testrsssrc
ATRSSSource *r = [ATRSSSource new];
r.name = @"NASA Large Image of the Day";
r.source = @"http://www.nasa.gov/rss/lg_image_of_the_day.rss";
r.localPath = @"/Users/alex/opt/nasa_lg_image_of_the_day/";
r.type = @"r";
[r getImagesFromRSS];
[r populateArrayFromLocalPath];
[self.sources addObject: r];
[r release];
}
}