2013-04-25 1 views
2

코코아 바인딩을 더 잘 이해하려고합니다. IB builder에서 NSArrayController로 작업하는 기본 테이블을 얻을 수 있습니다. 동일한 프로젝트를 사용하고 프로그래밍 방식으로 바인딩을 연결하려고 시도했지만 행이 표시되지 않습니다.프로그래밍 방식으로 NSTableView를 바인딩하십시오.

이 내 헤더 파일

@interface SBGoalDetailController : NSViewController <NSTableViewDelegate, NSTableViewDataSource> 

@property (nonatomic, strong) NSManagedObjectContext *gdcManagedObjectContext; 
@property (nonatomic, strong) NSArrayController *accountArrayController; 
@property (weak) IBOutlet NSTableView *accountTable; 


- (id)initWithContext:(NSManagedObjectContext *)context; 

그리고 내 구현 파일

@implementation SBGoalDetailController 

- (id)initWithContext:(NSManagedObjectContext *)context 
{ 
    self = [super initWithNibName:@"GoalDetailView" bundle:nil]; 
    if (self) { 
     [self setGdcManagedObjectContext:context]; 
    } 
    return self; 
} 



- (void)awakeFromNib 
{ 
    _accountArrayController = [[NSArrayController alloc] init]; 

    [[self accountArrayController] setManagedObjectContext:_gdcManagedObjectContext]; 
    [[self accountArrayController] setEntityName:@"Account"]; 
    [[self accountArrayController] setAutomaticallyPreparesContent:YES]; 
    [[self accountTable] bind:@"content" toObject:_accountArrayController withKeyPath:@"arrangedObjects" options:nil]; 

    [[self accountTable] bind:@"selectionIndexes" toObject:_accountArrayController withKeyPath:@"selectionIndexes" options:nil]; 
} 


- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row 
{ 

    NSView *returnView = [tableView makeViewWithIdentifier:@"AccountCell" owner:[tableView delegate]]; 

     NSTextField* textField = [[returnView subviews] objectAtIndex: 0]; 

    [textField bind: NSValueBinding 
      toObject: returnView 
     withKeyPath: @"objectValue.accountName" 
      options: nil]; 

    return returnView; 
} 

내가 부족 어떤 단계에서 어떤 제안입니까?

+0

안녕하세요, SO! – paulmelnikow

답변

3

감사합니다 Noa ArrayController 콘텐츠가 nil 인 경우이 섹션 Automatically Prepares Content flag을 우연히 발견하여 스타일을 지적하고 다음 awakeFromNib를 변경하십시오. 모두 작동하는 것 같습니다. 감사합니다.

- (void)awakeFromNib 
{ 
    [self setAccountArrayController:[[NSArrayController alloc] init]]; 

    [[self accountArrayController] setManagedObjectContext:[self gdcManagedObjectContext]]; 
    [[self accountArrayController] setEntityName:@"Account"]; 

    NSError *error = nil; 
    BOOL success = [[self accountArrayController] fetchWithRequest:nil merge:NO error:&error]; 
    if (success) { 
     [[self accountTable] bind:NSContentBinding toObject:[self accountArrayController] withKeyPath:@"arrangedObjects" options:nil]; 

     [[self accountTable] bind:NSSelectionIndexesBinding toObject:[self accountArrayController] withKeyPath:@"selectionIndexes" options:nil]; 
    } else { 
     NSLog(@"Error %@:", [error localizedDescription]); 
    } 
} 
+0

저는이 방법으로 작업하고 있습니다. ArrayController에 포함 된 객체가 지원/준수해야하는 것이 무엇인지 궁금하지 않습니다. 거기에 NSManagedObject's 있고 tableView 있지만 행을 표시하지만 값을 표시하지 않습니다 ... –

3

간단한 일들 먼저 : -awakeFromNib이 정확히 한 번 호출되고 해당 시간에 _gdcManagedObjectContextaccountTable이 0이 아닌지 확인하십시오. 당신은 문제가 있는지 확인 할 수 있도록보기에 정적 레이블 또는 배경 색상을 추가

에 한번 더 행 (눈에 보이지 않는 내용에 대 행).

문제가 행이 없음을 확인하면 -awakeFromNib에 문제가 있다고 결론을 내릴 수 있습니다. 어레이 컨트롤러의 arrangedObjects의 출력물을 추가하십시오. 아마 비어있을거야. 이론상으로 -tableView:viewForTableColumn:row의 코드가 아직 호출되지 않았습니다. 중단 점 또는 NSLog로 확인할 수 있습니다.

그런 경우 핵심 데이터 스택을 설정할 위치를 확인하십시오. NSPersistentDocument를 사용하고 있습니까? 관리 객체 컨텍스트가 작동하기 전에 실행 루프를 한 번 실행해야하는 문제가 발생했지만 여기서 볼 수있는 문제인지 여부를 검토해야합니다.

-tableView:viewForTableColumn:row에 코드에 문제가 있습니다. 이는 반복적으로 바인딩을 반복 설정하려는 것입니다. 셀 뷰의 각 인스턴스에 대해이 작업을 한 번 수행해야합니다. 코드에서 배열 컨트롤러를 설정하려고한다고해도, nib에서 셀 뷰의 서브 뷰를 바인딩하는 것이 좋습니다. 괜찮습니다. 또는 코드에서 수행하는 경우보기 당 한 번만 수행하는 방법을 찾아야합니다. 나는 그것이 당신의 문제를 일으키는 것이라고 생각하지 않습니다.

스타일 포인트 : 코드에 _accountArrayController_gdcManagedObjectContext 대신 self.accountArrayControllerself.gdcManagedObjectContext을 사용하십시오. 또한 다른 바인딩 유형 (예 : NSContentBindingNSSelectionIndexesBinding)에 대해 상수를 사용할 수 있습니다.