开发者

Setting up NSTableView with NSMutableArray of custom classes as data source

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 开发者_如何学Goto 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];
}

 }


I suspect it's a race condition in which the array is not populated correctly before NSTableView requests the display data, or something is messing with it in the meantime.

At any rate, there is probably a better way to do this using an NSArrayController. Apple has a detailed tutorial that can even do sub-arrays of the array (Master/Detail views). It's mostly some point and click work, and maybe as advanced as an override or two. Check it out at http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CocoaBindings/Tasks/masterdetail.html.

Using this method should also provide things like sorting and filtering for free, as well as easier maintenance (depending on your viewpoint).


Ok, so I finally got it, although I'm not entirely sure I understand it yet. You were right, in that it had something to do with sources being declared too many times, or something along those lines.

To get it to work, I needed to change [self sources] to [[NSApp delegate] sources]. I believe this makes sure that the correct sources object is being accessed. I realize that this is probably bad coding practice, and most of the tutorials suggest using a Singleton class instead, but this works for now and I'll clean it up later.

I'm not sure if the changes I made to the xib file made a difference. I have the app delegate linked to the table view as tableView, delegate, and dataSource. For some reason, [self sources] inside the objectValueForTableColumn does not reference the same sources object as [self sources] every other method, even though all of them are in the app delegate object.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜