使用NSArrayController绑定NSArray并在NSTableView中显示结果 [英] Bind an NSArray with NSArrayController and display the results in NSTableView

查看:170
本文介绍了使用NSArrayController绑定NSArray并在NSTableView中显示结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我处理小型核心数据,非基于文档的可可应用程序。在我的核心数据中有3个实体,它们具有一对一的关系。



实体可以在下面的图片上看到:



我可以管理显示在控制台日志中的值,但我不能在NSTableView中显示它们。



这是我的代码:



.h文件:



  #import< Cocoa / Cocoa.h> 
#importAppDelegate.h

@interface CombinedViewController:NSViewController< NSTableViewDataSource>

@property(nonatomic,strong)NSManagedObjectContext * mObjContext;
@property AppDelegate * appDelegate;
@property(strong)IBOutlet NSArrayController * combinedRecordsArrayController;
@property(nonatomic,strong)NSArray *网站;
@property(weak)IBOutlet NSTableView * combinedTableView;
@property(strong,nonatomic)NSString * hostingProvider;
@property(strong,nonatomic)NSString * customerName;
@property(strong,nonatomic)NSString * websiteUrl;

@end



.m档

  #importCombinedViewController.h
#importWebsite.h
#importCustomer.h
# importHosting.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:@WebsiteinManagedObjectContext: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(@网站:%@,客户:%@,托管提供商:%@,ws.websiteUrl,self.customerName,self.hostingProvider);
}

return self.websites;
}

@end

我有一个NSArrayController CombinedRecordsArrayController,在属性contoller有以下:Mode = Class,ClassName = CombinedViewContoller和两个复选框被选中,这些是准备内容和可编辑。在绑定,我已经设置了管理对象上下文文件ownder,模型关键路径self.mObjContext。



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



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



[valueForUndefinedKey:]:实体网站不是键值编码符合键hostingProvider。
任何帮助将深深赞赏。

解决方案

核心数据,NSArrayController和NSTableView是



数组控制器:

将模式设置为实体名称。

设置实体以实体名称命名。

检查准备内容和可编辑。

将受管对象上下文绑定到受管对象上下文。



表视图:

将内容绑定到数组控制器,控制器键arrange objects。

将选择索引绑定到数组控制器,控制器键选择索引。

将排序描述符绑定到数组控制器,控制器键sortDescriptors。

将表视图单元格的值绑定到表单元视图,模型键路径objectValue.websiteUrl。检查有条件地设置可编辑。



就是这样。


I work on trivial core data, non document based cocoa app. There are 3 entities in my core data, and they have one to one relationship.

Entities can be seen on the image bellow:

I can manage to display the values in the console log, but I can't display them in the NSTableView.

Here is my code:

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

@end

.m file

#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

I have an NSArrayController, named CombinedRecordsArrayController, which in attributes contoller has the following: Mode = Class, ClassName = CombinedViewContoller and both checkboxes are checked, those are Prepares Content and Editable. In binding, I have set the manage object context to file ownder, Model Key Path self.mObjContext.

NSTable is binded with this NSArrayContoller, and if I left only the first column, that is websiteUrl, I get the results.

NSLog line in my .m file, do print the results. However app crash with following error:

[ valueForUndefinedKey:]: the entity Website is not key value coding-compliant for the key "hostingProvider". Any help will be deeply appreciated. I am struggling with this 4-5 days, and I can't solve it.

解决方案

Core Data, NSArrayController and NSTableView is easy, if you know how to do it.

Array Controller:
Set Mode to Entity Name.
Set Entity Name to the name of the entity.
Check Prepares Content and Editable.
Bind Managed Object Context to the managed object context.

Table View:
Bind Content to the array controller, Controller Key arrangedObjects.
Bind Selection Indexes to the array controller, Controller Key selectionIndexes.
Bind Sort Descriptors to the array controller, Controller Key sortDescriptors.
Bind Value of the table view cell to Table Cell View, Model Key Path objectValue.websiteUrl. Check Conditionally Sets Editable.

That's it.

这篇关于使用NSArrayController绑定NSArray并在NSTableView中显示结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆