我有一个返回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 ...没有错误显示在控制台上。
我改变了_websites与self.websites在我使用它的2行,而是一个空的nstableview,我现在得到错误:实体网站是不符合密钥值的关键字“customerName”的密钥值。还有什么我应该改变我的代码? – user2417624
您似乎在您的代码中使用了customerInfo,而不是customerName – stevesliva
这就是for循环,如果我在那里使用customerName,则显示错误。我无法看到任何其他地方我有customerInfo。 CustomerInfo是关系的名称,(一对一) – user2417624