2013-04-25 68 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 2013-04-25 21:05:27

回答

3

感謝Noa的ArrayController內容爲零,然後我偶然發現了這個部分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'在那裏和tableView顯示行,但不顯示值... – 2014-06-23 16:27:53

3

簡單的事情第一:確保-awakeFromNib只被調用一次,並且_gdcManagedObjectContextaccountTable當時是非零。

嘗試添加靜態標籤或背景顏色到您的視圖,以便您可以確認問題是沒有行(與行與無形的內容)。

當您確認問題沒有行時,您可以得出結論-awakeFromNib中存在問題。嘗試添加陣列控制器的打印輸出arrangedObjects。這可能是空的。理論上你的代碼-tableView:viewForTableColumn:row尚未被調用。你可以用斷點或NSLog來確認。

如果是這樣,請檢查您設置Core Data堆棧的位置。你在使用NSPersistentDocument嗎?我遇到了一個問題,即在託管對象上下文開始工作之前,運行循環需要運行一次,但是我不得不考慮這是否是您在此處看到的問題。

您的代碼在-tableView:viewForTableColumn:row中存在問題,即您可能一遍又一遍地設置綁定。您應該爲單元格視圖的每個實例執行一次此操作。即使你想在代碼中設置數組控制器,我建議你考慮綁定單元格視圖的子視圖,因爲它就在那裏。或者,如果您使用代碼執行此操作,則您需要找到一種方法,只在每個視圖中執行一次。不過,我認爲這不會導致你的問題。

文體要點:在您的代碼中,請使用self.accountArrayControllerself.gdcManagedObjectContext而不是_accountArrayController_gdcManagedObjectContext。另外,您可以使用其他綁定類型的常量:NSContentBindingNSSelectionIndexesBinding

相關問題