2015-10-16 79 views
0

我有一个返回2结果(我可以看到使用NSLog记录)的数组(_websites)。无法与NSArrayController绑定NSArray

我想要做的是在NSTableView中显示那些有3列的2条记录。我做了大量的尝试,将我的数组的内容与NSArrayController绑定,没有任何成功。

这里是.h文件

#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 

.m文件代码:

#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 

我想学习如何做左右逢源,使用可可绑定和编程,因此任何将不胜感激。带有一些最新教程的链接也将非常受欢迎。

我只是忘了提及...我在控制器内容中绑定NSArrayController与ContentArray,然后我绑定NSTableView与NSArrayController,以及我的表列,但我得到空NSTableView ...没有错误显示在控制台上。

回答

1

如果您使用直接iVar访问 - _websites - 编写websites属性的iVar,绑定依赖的KVO通知从不发生。

如果您改为使用self.websites =或更多明确[self setWebsites: ...,那么您将触发KVO通知给阵列控制器,网站属性的值已更新。

相反,在XIB阵列控制器是未归档和viewDidLoad之前绑定到websites,所以在这一点上,是websitesnil。随后,您永远不会触发任何有关websites更改值的KVO通知,因为您明确避免使用websites访问者setWebsites,而是使用直接实例变量访问。所以AC不知道websites发生了变化,表格从未反映websites的任何值,但nil除外。

一般情况下,除非您有足够的理由这样做并完全理解您为什么这么做,否则绝对不要使用实例变量来访问属性的值。

+0

我改变了_websites与self.websites在我使用它的2行,而是一个空的nstableview,我现在得到错误:实体网站是不符合密钥值的关键字“customerName”的密钥值。还有什么我应该改变我的代码? – user2417624

+0

您似乎在您的代码中使用了customerInfo,而不是customerName – stevesliva

+0

这就是for循环,如果我在那里使用customerName,则显示错误。我无法看到任何其他地方我有customerInfo。 CustomerInfo是关系的名称,(一对一) – user2417624

相关问题