gpt4 book ai didi

objective-c - 将 NSArray 与 NSArrayController 绑定(bind)并在 NSTableView 中显示结果

转载 作者:行者123 更新时间:2023-12-03 16:35:15 25 4
gpt4 key购买 nike

我从事琐碎的核心数据、非基于文档的 cocoa 应用程序。我的核心数据中有3个实体,它们具有一对一的关系。

实体可以在下面的图像中看到:enter image description here

我可以设法在控制台日志中显示值,但无法在 NSTableView 中显示它们。

这是我的代码:

.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 *combinedTableView;
@property(strong,nonatomic)NSString *hostingProvider;
@property(strong,nonatomic)NSString *customerName;
@property(strong,nonatomic)NSString *websiteUrl;

@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;
}

-(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

我有一个名为CombinedRecordsArrayController的NSArrayController,它在属性 Controller 中具有以下内容:Mode = Class,ClassName = CombinedViewContoller,并且两个复选框都被选中,它们是准备内容和可编辑的。在绑定(bind)中,我已将管理对象上下文设置为文件所有者、模型 key 路径 self.mObjContext。

NSTable 与此 NSArrayContoller 绑定(bind),如果我只留下第一列,即 websiteUrl,我会得到结果。

我的 .m 文件中的 NSLog 行,打印结果。但是应用程序崩溃并出现以下错误:

[ valueForUndefinedKey:]:实体 Website 不符合键“hostingProvider”的键值编码。任何帮助将不胜感激。我苦苦挣扎了4-5天,但无法解决。

最佳答案

核心数据、NSArrayController 和 NSTableView 很简单,如果你知道怎么做的话。

数组 Controller :
将模式设置为实体名称。
将实体名称设置为实体的名称。
检查准备内容并可编辑。
将托管对象上下文绑定(bind)到托管对象上下文。

表格 View :
将Content绑定(bind)到数组 Controller ,Controller Key排列的Objects。
将选择索引绑定(bind)到数组 Controller , Controller 键选择索引。
将排序描述符绑定(bind)到数组 Controller ,Controller Key sortDescriptors。
将 TableView 单元格的值绑定(bind)到表单元格 View ,模型键路径objectValue.websiteUrl。选中有条件设置可编辑。

就是这样。

关于objective-c - 将 NSArray 与 NSArrayController 绑定(bind)并在 NSTableView 中显示结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33271673/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com