Question

I have some code which needs to access a NSArray to work. I have a NSArray which I am using with Core Data and will have data in it, but I am unsure how to make my NSArrayController access the NSArray.

I can't just simply declare it in the Header file like this: NSArray *objectArray; because it does not know how or which NSArray to access. How exactly would I access the NSArray I am using with Core Data?

My Header File:

#import <Cocoa/Cocoa.h>


@interface MyOutlineView : NSOutlineView {
    NSArrayController* objectArray;
}

@end

My Implementation File:

#import "MyOutlineView.h"

@implementation MyOutlineView

- (void) outlineView: (NSOutlineView *) aView
     willDisplayCell: (id) aCell
      forTableColumn: (NSTableColumn *)aColumn
                item: (id) anItem
{
    id rootObj = anItem;
    unsigned row = [aView rowForItem:anItem];

    [aCell setDrawsBackground: YES];

    while ([aView levelForRow:row] != 0) {
        row --;
        rootObj = [aView itemAtRow:row];
    }

    // The colours here are foul and ugly.  Use something else, for
    // God's sake!
    if( [objectArray indexOfObject:rootObj] % 2 )
        [aCell setBackgroundColor: [NSColor yellowColor]];
    else
        [aCell setBackgroundColor: [NSColor blueColor]];
}

@end
Was it helpful?

Solution

I've made a test app with IBOutlet connected to NSArrayController from Xib. In this test I have:

  • started from Core Data application template;
  • created Entity in data model with two attributes (string, int);

in Xib:

  • Array Controller with managed object context, connected to Test_AppDelegate.managedObjectContext;
  • TableView with cols connected to Array Controller's first and second attribute of arrangedObjects;
  • Add and Remove buttons, connected to Array Controller's add: and remove: actions;
  • Button "Show Count" and Label; -

in code (Test_AppDelegate.*):

  • IBOutlet NSArrayController *ac; (connected in Xib from Test_AppDelegate.ac to Array Controller);
  • IBOutlet NSTextField *nLabel; (connected in Xib to Label);
  • (IBAction)showNum:(id)sender; (connected from "Show Count" button);
  • code in action showNum: [nLabel setIntValue:[[ac arrangedObjects] count]];

I'm able to:

  • Add/Remove objects to table view and controlled array;
  • Access NSArrayController from code to get arrangedObjects array.

So binding IBOutlet from code to Xib's Array Controller and accessing its arrangedObjects should work.

OTHER TIPS

I might not be getting this - but can't you just create an initWithArray method?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top