【问题标题】:How can I rebind the NSValueBinding of an NSTableColumn?如何重新绑定 NSTableColumn 的 NSValueBinding?
【发布时间】:2013-08-17 06:05:13
【问题描述】:

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

表格列最初绑定到 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
)

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

【问题讨论】:

    标签: cocoa nstableview cocoa-bindings nsarraycontroller


    【解决方案1】:

    在设置新绑定之前尝试调用-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];
        }
    }
    

    另外,您是否验证了数组控制器都包含它们应该包含的对象。 IE。 productsArrayController 是否包含 SKProductssongsArrayController 是否包含 Song(或任何真正的类)对象?

    【讨论】:

    • 谢谢安德鲁。我已经验证了这两个数组控制器包含预期的对象类,并且我还尝试在重新绑定之前取消绑定。我最终创建了第二个 NSTableView,所以我现在有一个用于歌曲,另一个用于产品,并根据需要在 segmentedControlChanged: 中显示/隐藏它们。我仍然很好奇为什么重新绑定不起作用。
    • 是的,我也想知道。如果没有看到更多代码,很难说更多。不过,拥有两个 NSTableView,也许在一个表格 NSTabView 中,是一个很好的解决方案。
    猜你喜欢
    • 2023-03-22
    • 1970-01-01
    • 2013-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多