Objective c 使用NSArray控制器绑定NSArray,并在NSTableView中显示结果
我工作于琐碎的核心数据,非基于文档的cocoa应用程序。我的核心数据中有3个实体,它们之间有一对一的关系 实体可以在下图中看到: 我可以设法在控制台日志中显示这些值,但无法在NSTableView中显示它们 这是我的密码: .h文件: 我有一个名为CombinedRecordsArrayController的NSArrayController,它在attributes控件中具有以下内容:Mode=Class,ClassName=CombinedViewController,并且选中了这两个复选框,即准备内容和可编辑。在绑定中,我将管理对象上下文设置为文件所有者Model Key Path self.mObjContext NSTable与这个NSArrayContoller绑定,如果我只留下第一列,即websiteUrl,我将得到结果 在my.m文件中的NSLog行中,打印结果。但是,应用程序崩溃,出现以下错误: [valueForUndefinedKey:]:实体网站不符合密钥“hostingProvider”的键值编码。Objective c 使用NSArray控制器绑定NSArray,并在NSTableView中显示结果,objective-c,cocoa,core-data,cocoa-bindings,Objective C,Cocoa,Core Data,Cocoa Bindings,我工作于琐碎的核心数据,非基于文档的cocoa应用程序。我的核心数据中有3个实体,它们之间有一对一的关系 实体可以在下图中看到: 我可以设法在控制台日志中显示这些值,但无法在NSTableView中显示它们 这是我的密码: .h文件: 我有一个名为CombinedRecordsArrayController的NSArrayController,它在attributes控件中具有以下内容:Mode=Class,ClassName=CombinedViewController,并且选中了这两个复选框
任何帮助都将不胜感激。我在这4-5天里苦苦挣扎,但我无法解决它。我认为您需要为数据模型生成NSManagedObject子类。如果没有,请加载数据模型文件并在编辑器菜单下查找该选项
如果不是这样,请查看,因为它特别提到了您的错误和一些解决方案。祝你好运,伙计,如果有什么效果,请告诉我。4-5天……我感觉到你的痛苦 核心数据、NSArrayController和NSTableView都很简单,如果您知道如何操作的话 阵列控制器:
将模式设置为实体名称。
将实体名称设置为实体的名称。
选中“准备内容并可编辑”。
将托管对象上下文绑定到托管对象上下文 表视图:
将内容绑定到阵列控制器、控制器键arrangedObjects。
将选择索引绑定到阵列控制器、控制器键选择索引。
将排序描述符绑定到阵列控制器、控制器密钥排序描述符。
将表视图单元格的值绑定到表单元格视图,模型键路径objectValue.websiteUrl。选中有条件的可编辑集
就是这样。不,您不需要生成NSManagedObject子类。我已经完成了,我所有的实体都是子类的。这里的问题如下:在nstable视图中,我有3列。每一列,都应该从不同的实体中填写。我不能做最后一步。我没有表视图单元格,我有表列和表单元格。我无法将其绑定到表格单元格视图,我的选项是:文件所有者、应用程序、ArrayController和共享用户默认值……我想最后一步是针对iOS,而不是OS-X……如果您能澄清最后一步,那将是非常棒的,以防我的问题不清楚:在nstable视图中,我有3列。每一列,应填写不同的实体。字段customer name在customer实体中,字段providerName在Hosing实体中,websiteUrl在website实体中。更新:我找到了如何执行上一步的方法,我必须切换到基于视图来执行此操作,但我仍会执行相同的错误:[valueForUndefinedKey:]:实体网站不符合密钥的键值编码“hostingProvider”。请阅读我之前的评论。您必须使用网站实体的关键路径:customerInfo.customerName hostingInfo.providerName websiteUrlYep,就是这样。谢谢!
#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 *combinedTableView;
@property(strong,nonatomic)NSString *hostingProvider;
@property(strong,nonatomic)NSString *customerName;
@property(strong,nonatomic)NSString *websiteUrl;
@end
#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;
}
-(void)viewDidAppear {
[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);
}
self.websites = [self.mObjContext executeFetchRequest:fetchRequest error:nil];
for (Website *ws in self.websites) {
self.hostingProvider = ws.hostingInfo.hostingProvider;
self.customerName = ws.customerInfo.customerName;
NSLog(@"Website: %@, Customer: %@, Hosting Provider: %@", ws.websiteUrl, self.customerName, self.hostingProvider);
}
return self.websites;
}
@end