如何重新绑定NSTableColumn的NSValueBinding?

时间:2013-08-16 16:10:22

标签: cocoa nstableview cocoa-bindings nsarraycontroller

我正在构建一个Mac OS X应用程序,它具有带有单个NSTableColumn和NSSegmentedControl的NSTableView。当我选择不同的段时,我打算切换表视图以显示来自不同阵列控制器的行。

表格列最初绑定到self.songsArrayController,其中IB中的密钥路径为arrangedObjects.name。当选定的段发生如下变化时,我正在重新绑定表列的值绑定:

- (IBAction) segmentedControlChanged:(NSSegmentedControl*) sender {
    if (sender.selectedSegment == 0) {
        [self.songNameTableColumn bind:NSValueBinding toObject:self.songsArrayController withKeyPath:@"arrangedObjects.name" options:nil];
    } else if (sender.selectedSegment == 1) {
        [self.songNameTableColumn bind:NSValueBinding toObject:self.productsArrayController withKeyPath:@"arrangedObjects.localizedTitle" options:nil];
    }
}

当我点击第二段时,我看到以下错误:

2013-08-16 08:55:26.245 MyApp[76005:303] [<SKProduct 0x10b3264d0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key name.
2013-08-16 08:55:26.270 MyApp[76005:303] (
    0   CoreFoundation                      0x00007fff96d69b06 __exceptionPreprocess + 198
    1   libobjc.A.dylib                     0x00007fff8c7ea3f0 objc_exception_throw + 43
    2   CoreFoundation                      0x00007fff96dfdf49 -[NSException raise] + 9
    3   Foundation                          0x00007fff977ac15c -[NSObject(NSKeyValueCoding) valueForUndefinedKey:] + 238
    4   Foundation                          0x00007fff976f3ea9 -[NSObject(NSKeyValueCoding) valueForKey:] + 400
    5   Foundation                          0x00007fff9771474c -[NSObject(NSKeyValueCoding) valueForKeyPath:] + 341
    6   Foundation                          0x00007fff97714713 -[NSObject(NSKeyValueCoding) valueForKeyPath:] + 284
    7   AppKit                              0x00007fff8dbf70a4 -[NSBinder valueForBinding:resolveMarkersToPlaceholders:] + 163
    8   AppKit                              0x00007fff8dbf99f7 -[NSValueBinder _adjustObject:mode:observedController:observedKeyPath:context:editableState:adjustState:] + 667
    9   AppKit                              0x00007fff8dbf964c -[NSValueBinder _observeValueForKeyPath:ofObject:context:] + 192
    10  AppKit                              0x00007fff8dbfe0ab -[NSTextValueBinder _observeValueForKeyPath:ofObject:context:] + 43
    11  Foundation                          0x00007fff977167b7 NSKeyValueNotifyObserver + 390
    12  Foundation                          0x00007fff977181c1 NSKeyValueDidChange + 456
    13  Foundation                          0x00007fff976d376a -[NSObject(NSKeyValueObserverNotification) didChangeValueForKey:] + 118
    14  AppKit                              0x00007fff8dc3bae6 -[NSTableRowData _addViewToRowView:atColumn:row:] + 696
    15  AppKit                              0x00007fff8dc3b63f -[NSTableRowData _addViewsToRowView:atRow:] + 151
    16  AppKit                              0x00007fff8dc39bd5 -[NSTableRowData _addRowViewForVisibleRow:withPriorView:] + 415
    17  AppKit                              0x00007fff8dc3995a -[NSTableRowData _addRowViewForVisibleRow:withPriorRowIndex:inDictionary:withRowAnimation:] + 272
    18  AppKit                              0x00007fff8dc38c29 -[NSTableRowData _unsafeUpdateVisibleRowEntries] + 740
    19  AppKit                              0x00007fff8dc387c1 -[NSTableRowData updateVisibleRowViews] + 119
    20  AppKit                              0x00007fff8dc105a7 -[NSTableView layout] + 165
    21  AppKit                              0x00007fff8dbc3e65 -[NSView _layoutSubtreeHeedingRecursionGuard:] + 112
    22  CoreFoundation                      0x00007fff96d624a6 __NSArrayEnumerate + 582
    23  AppKit                              0x00007fff8dbc3fc6 -[NSView _layoutSubtreeHeedingRecursionGuard:] + 465
    24  CoreFoundation                      0x00007fff96d624a6 __NSArrayEnumerate + 582
    25  AppKit                              0x00007fff8dbc3fc6 -[NSView _layoutSubtreeHeedingRecursionGuard:] + 465
    26  CoreFoundation                      0x00007fff96d624a6 __NSArrayEnumerate + 582
    27  AppKit                              0x00007fff8dbc3fc6 -[NSView _layoutSubtreeHeedingRecursionGuard:] + 465
    28  CoreFoundation                      0x00007fff96d624a6 __NSArrayEnumerate + 582
    29  AppKit                              0x00007fff8dbc3fc6 -[NSView _layoutSubtreeHeedingRecursionGuard:] + 465
    30  CoreFoundation                      0x00007fff96d624a6 __NSArrayEnumerate + 582
    31  AppKit                              0x00007fff8dbc3fc6 -[NSView _layoutSubtreeHeedingRecursionGuard:] + 465
    32  CoreFoundation                      0x00007fff96d624a6 __NSArrayEnumerate + 582
    33  AppKit                              0x00007fff8dbc3fc6 -[NSView _layoutSubtreeHeedingRecursionGuard:] + 465
    34  CoreFoundation                      0x00007fff96d624a6 __NSArrayEnumerate + 582
    35  AppKit                              0x00007fff8dbc3fc6 -[NSView _layoutSubtreeHeedingRecursionGuard:] + 465
    36  AppKit                              0x00007fff8dbc3cfe -[NSView layoutSubtreeIfNeeded] + 615
    37  AppKit                              0x00007fff8dbbf4ac -[NSWindow(NSConstraintBasedLayout) layoutIfNeeded] + 201
    38  AppKit                              0x00007fff8dab90a8 _handleWindowNeedsDisplayOrLayoutOrUpdateConstraints + 446
    39  Foundation                          0x00007fff976fe463 __NSFireTimer + 96
    40  CoreFoundation                      0x00007fff96d26804 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 20
    41  CoreFoundation                      0x00007fff96d2631d __CFRunLoopDoTimer + 557
    42  CoreFoundation                      0x00007fff96d0bad9 __CFRunLoopRun + 1529
    43  CoreFoundation                      0x00007fff96d0b0e2 CFRunLoopRunSpecific + 290
    44  HIToolbox                           0x00007fff8f1c2eb4 RunCurrentEventLoopInMode + 209
    45  HIToolbox                           0x00007fff8f1c2c52 ReceiveNextEventCommon + 356
    46  HIToolbox                           0x00007fff8f1c2ae3 BlockUntilNextEventMatchingListInMode + 62
    47  AppKit                              0x00007fff8dab6533 _DPSNextEvent + 685
    48  AppKit                              0x00007fff8dab5df2 -[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:] + 128
    49  AppKit                              0x00007fff8daad1a3 -[NSApplication run] + 517
    50  AppKit                              0x00007fff8da51bd6 NSApplicationMain + 869
    51  MyApp                               0x0000000100001fc2 main + 34
    52  libdyld.dylib                       0x00007fff97d227e1 start + 0
)

看来即使我使用密钥路径self.productsArrayController将NSValueBinding重新绑定到arrangedObjects.localizedTitle,表格列仍在尝试使用arrangedObjects.name的旧密钥路径来提取数据。有谁知道为什么会发生这种情况,以及如何正确地将现有的NSTableColumn重新绑定到不同的阵列控制器和关键路径?

1 个答案:

答案 0 :(得分:0)

在设置新绑定之前尝试调用-unbind:

- (IBAction) segmentedControlChanged:(NSSegmentedControl*) sender {
    if (sender.selectedSegment == 0) {
        [self.songNameTableColumn unbind:NSValueBinding];
        [self.songNameTableColumn bind:NSValueBinding toObject:self.songsArrayController withKeyPath:@"arrangedObjects.name" options:nil];
    } else if (sender.selectedSegment == 1) {
        [self.songNameTableColumn unbind:NSValueBinding];
        [self.songNameTableColumn bind:NSValueBinding toObject:self.productsArrayController withKeyPath:@"arrangedObjects.localizedTitle" options:nil];
    }
}

此外,您是否已验证阵列控制器都包含他们应该使用的对象。即。 productsArrayController包含SKProductssongsArrayController包含Song(或真正的类)对象吗?