2012-08-28 6 views
1

원래 콘센트를 통해 내 NSTableView를 채우고 컨트롤러 클래스에 테이블의 dataSource를 설정했습니다. 내 응용 프로그램에서 열 정렬 할 수 있도록 NSArrayController 사용하여 전환 할 노력하고있어.NSArrayController를 통해 코어 데이터에서 NSTableView를 채우려면 어떻게해야합니까?

IB에서는 Array Controller 개체를 추가했습니다. Sort Descriptors 바인딩을 Shared User Defaults Controller에 연결하여 정렬 된 열을 내 앱 시작 사이에 유지할 수 있습니다. 배열 테이블에있는 각 열의 배열 컨트롤러에 바인딩 된 컨트롤러 키가 'arrangedObjects'로 설정되고 모델 키 경로가 렌더링되어야하는 필드의 이름으로 설정됩니다.

내 데이터가 핵심 데이터에서 나오고 표시하려고하는 엔티티가 다른 엔티티와 관계가 있습니다. 두 번째 엔티티의 속성은 테이블 열 중 하나의 값으로 표시되어야합니다. 누구나 내가 여기 놓친 것에 대해 어떤 생각/제안을 가지고 있니?

MainWindowController.h

#import <Cocoa/Cocoa.h> 
#import "Notification.h" 

@class AppDelegate; 

@interface MainWindowController : NSWindowController <NSTableViewDataSource, NSTableViewDelegate> { 
    AppDelegate <NSApplicationDelegate> *appDelegate; 
} 

//@property NSMutableArray *userNotifications; 

@property (weak) IBOutlet NSTableView *notificationsTable; 
@property (weak) IBOutlet NSArrayController *notificationsController; 

@end 

MainWindowController.m

#import "AppDelegate.h" 
#import "MainWindowController.h" 
#import "Utils.h" 

@implementation MainWindowController 

//@synthesize userNotifications; 

@synthesize notificationsTable; 
@synthesize notificationsController; 

- (void) doubleClick:(id)sender 
{ 
    NSInteger row = [notificationsTable clickedRow]; 

// Notification *clickedNotification = [userNotifications objectAtIndex:row]; 
// Notification *clickedNotification = 

// [appDelegate redirectToBrowser:clickedNotification]; 
} 

- (id) initWithWindowNibName:(NSString *)windowNibName 
{ 
    self = [super initWithWindowNibName:windowNibName]; 
    if (self) { 
//  userNotifications = [[NSMutableArray alloc] init]; 
     appDelegate = (AppDelegate *) [[NSApplication sharedApplication] delegate]; 
     [notificationsController setManagedObjectContext:[appDelegate managedObjectContext]]; 
     [notificationsController setEntityName:@"Notification"]; 
     [notificationsController setAutomaticallyPreparesContent:YES]; 

     [notificationsController fetch:self]; 
     [notificationsTable reloadData]; 
    } 
    return self; 
} 

- (void)windowDidLoad 
{ 
    [super windowDidLoad]; 

    // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file. 

    [notificationsTable reloadData]; 
} 

- (void)awakeFromNib 
{ 
    [notificationsTable setTarget:self]; 
    [notificationsTable setDoubleAction:@selector(doubleClick:)]; 
} 

답변

1

이 문제에 대한 두 가지 해결책이 있습니다

  1. 새로운 객체를 추가하여 IB에 앱 위임 객체를 추가하기 클래스를 AppDelegate으로 설정하십시오.
  2. 인스턴스 변수 당신 .H와하는 .m 파일에 아래 코드를 추가하여 창을 렌더링 컨트롤러의 AppDelegate에로 앱 위임을 할당합니다
    @class AppDelegate; 
    
    @interface Controller : NSWindowController { 
        AppDelegate <NSApplicationDelegate> *appDelegate; 
    } 
    

    Controller.h

Controller.m

#import "AppDelegate.h" 

- (id) initWithWindowNibName:(NSString *)windowNibName 
{ 
    self = [super initWithWindowNibName:windowNibName]; 
    if (self) { 
     appDelegate = (AppDelegate *) [[NSApplication sharedApplication] delegate]; 
    } 
    return self; 
} 

두 경우 모두 어레이 컨트롤러에 대한 새 바인딩을 추가해야합니다. Inspector의 Binding 창으로 이동하여 관리되는 객체 컨텍스트 (매개 변수 아래)를 App Delegate에 바인딩 (1)하거나 파일 소유자 (2)로 설정 한 다음 모델 키 경로를 (1) self.managedObjectContext 또는 2) self.appDelegate.managedObjectContext

관련 문제