简体   繁体   中英

Can't bind NSArray with NSArrayController

I have an array (_websites) which returns 2 results (i can see the records using NSLog).

What I am trying to do is to display those 2 records in NSTableView that has 3 columns. I make numerous attempts to bind the content of my array with the NSArrayController, without any success.

Here is the .h file

#import <Cocoa/Cocoa.h>
#import "AppDelegate.h"

@interface CombinedViewController : NSViewController <NSTableViewDataSource>
@property (nonatomic,strong) NSManagedObjectContext *mObjContext;
@property AppDelegate *appDelegate;
@property (strong) IBOutlet NSArrayController *combinedRecordsArrayController;
@property (nonatomic,strong)NSArray *websites;
@property (weak) IBOutlet NSTableView *tableView;

@end

the .m file code:

#import "CombinedViewController.h"
#import "Website.h"
#import "Customer.h"
#import "Hosting.h"

@interface CombinedViewController ()

@end

@implementation CombinedViewController

- (void)viewDidLoad {

    [super viewDidLoad];
    _appDelegate = (AppDelegate*)[[NSApplication sharedApplication] delegate];
    self.mObjContext = _appDelegate.managedObjectContext;
    [self getCombinedResutls];    

}

-(NSArray *)getCombinedResutls {

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Website" inManagedObjectContext:self.mObjContext];
    [fetchRequest setEntity:entity];
    NSError *error = nil;
    NSArray *fetchedObjects = [self.mObjContext executeFetchRequest:fetchRequest error:&error];
    if (fetchedObjects == nil) {
        NSLog(@"Error:%@",error);
    }
    _websites = [_mObjContext executeFetchRequest:fetchRequest error:nil];

    for (Website *ws in _websites) {
        Customer *cust = ws.customerInfo;
        Hosting *host = ws.hostingInfo;

        NSLog(@"Website: %@, Customer: %@, Hosting Provider: %@",ws.websiteUrl, cust.customerName, host.hostingProvider);

    }

return fetchedObjects;
}


@end

I am trying to learn how to do it both ways, using cocoa binding and programmatically, so any kind solution will be appreciated. Links with some up to date tutorial will be also very welcomed.

I just forgot to mention...I bind the NSArrayController with ContentArray in Controller Content, and then I bind the NSTableView with the NSArrayController, as well as my Table Column,but I am getting empty NSTableView...no error is shown in console whatsoever.

If you use direct iVar access-- _websites -- to write the websites property's iVar, the KVO notification that the binding depends upon never happens.

If you instead use self.websites = or more explicitly [self setWebsites: ... , then you will trigger a KVO notification to the array controller that the value of the websites property has been updated.

Instead, the array controller in the Xib is unarchived and bound to websites before viewDidLoad , so at that point, websites is nil . And subsequently, you never trigger any KVO notification about websites changing value because you explicitly avoid using the websites accessor setWebsites and instead use direct instance variable access. So the AC never knows that websites changes and the table never reflects any value for websites except nil .

In general never use the instance variable to access a property's value unless you have a very good reason to do so and fully understand why you're doing so.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM