【问题标题】:Cocoa bindings - Sorting error for a specific column可可绑定 - 特定列的排序错误
【发布时间】:2019-11-09 09:51:04
【问题描述】:

我使用绑定到 NSTreeController 的 NSOutlineView。 我成功地对视图的所有列进行了排序,除了一个! 如果binaryData 字段不为零,则此列显示一个启用的按钮。 模型中的 binaryData 字段与具有 NSData? 字段的 MyBinary NSManagedObject 子类相关。我按照建议使用这种方法(一种关系)来避免在不需要时将所有 NSData 加载到内存中。

我希望此列可排序,并且在单击时将所有启用的按钮重新组合(升序或降序),并将所有禁用的按钮重新组合。

在 IB 中,与其他列一样,我将列值绑定到:

  • 控制器键 = 排列对象
  • 模型键路径 = binaryData

但是当我点击该列时,我有以下堆栈:

2019-11-09 10:31:44.713177+0100 MyApp[71910:2872832] -[MyApp.MyBinary compare:]: unrecognized selector sent to instance 0x6000021429e0
2019-11-09 10:31:44.713628+0100 MyApp[71910:2872832] [General] -[MyApp.MyBinary compare:]: unrecognized selector sent to instance 0x6000021429e0
2019-11-09 10:31:44.717803+0100 MyApp[71910:2872832] [General] (
    0   CoreFoundation                      0x00007fff36294f53 __exceptionPreprocess + 250
    1   libobjc.A.dylib                     0x00007fff6c35a835 objc_exception_throw + 48
    2   CoreFoundation                      0x00007fff3631f106 -[NSObject(NSObject) __retain_OA] + 0
    3   CoreFoundation                      0x00007fff3623b6cb ___forwarding___ + 1427
    4   CoreFoundation                      0x00007fff3623b0a8 _CF_forwarding_prep_0 + 120
    5   Foundation                          0x00007fff388a1a44 _NSCompareObject + 46
    6   CoreFoundation                      0x00007fff36206288 __CFSimpleMergeSort + 74
    7   CoreFoundation                      0x00007fff362061a6 CFSortIndexes + 390
    8   CoreFoundation                      0x00007fff36223720 CFMergeSortArray + 290
    9   Foundation                          0x00007fff388a179b _sortedObjectsUsingDescriptors + 592
    10  Foundation                          0x00007fff388a1397 -[NSArray(NSKeyValueSorting) sortedArrayUsingDescriptors:] + 317
    11  AppKit                              0x00007fff33666373 -[NSTreeNode sortWithSortDescriptors:recursively:] + 461
    12  AppKit                              0x00007fff336664be -[NSTreeNode sortWithSortDescriptors:recursively:] + 792
    13  AppKit                              0x00007fff33666073 -[NSTreeController setSortDescriptors:] + 304
    14  Foundation                          0x00007fff388e1ce3 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 363
    15  AppKit                              0x00007fff336695bb -[NSBinder _setValue:forKeyPath:ofObject:mode:validateImmediately:raisesForNotApplicableKeys:error:] + 445
    16  AppKit                              0x00007fff336693aa -[NSBinder setValue:forBinding:error:] + 236
    17  AppKit                              0x00007fff33ab0359 -[NSOutlineViewBinder tableView:didChangeToSortDescriptors:] + 119
    18  AppKit                              0x00007fff337c10cc -[_NSBindingAdaptor tableView:didChangeToSortDescriptors:] + 152
    19  AppKit                              0x00007fff3366bba8 -[NSTableView setSortDescriptors:] + 258
    20  AppKit                              0x00007fff33ba44e8 -[NSTableView _changeSortDescriptorsForClickOnColumn:] + 536
    21  AppKit                              0x00007fff33b8a127 -[NSTableHeaderView _trackAndModifySelectionWithEvent:onColumn:stopOnReorderGesture:] + 999
    22  AppKit                              0x00007fff33b8d24a -[NSTableHeaderView mouseDown:] + 546
    23  AppKit                              0x00007fff335f25e9 -[NSWindow(NSEventRouting) _handleMouseDownEvent:isDelayedEvent:] + 4907
    24  AppKit                              0x00007fff33535eb0 -[NSWindow(NSEventRouting) _reallySendEvent:isDelayedEvent:] + 2612
    25  AppKit                              0x00007fff3353523d -[NSWindow(NSEventRouting) sendEvent:] + 349
    26  AppKit                              0x00007fff333f945c -[NSApplication(NSEvent) sendEvent:] + 352
    27  AppKit                              0x00007fff333e8da7 -[NSApplication run] + 707
    28  AppKit                              0x00007fff333da95d NSApplicationMain + 777
    29  MyApp                                0x000000010002b02d main + 13
    30  libdyld.dylib                       0x00007fff6d6bd2e5 start + 1
    31  ???                                 0x0000000000000003 0x0 + 3
)

我该如何解决这个问题?我想过使用自定义排序描述符,但我不知道如何告诉 OutlineView 或 TreeController 为特定列使用特定描述符。

感谢您的帮助!

【问题讨论】:

    标签: core-data nstableview nsoutlineview nssortdescriptor nstreecontroller


    【解决方案1】:

    如何告诉 OutlineView 或 TreeController 为特定列使用特定描述符

    设置表格列的sortDescriptorPrototype

    【讨论】:

    • 我可以在这条线索上走得更远,谢谢......但我有时仍然会收到错误MyApp.MyBinary compare:]: unrecognized selector sent to instance 0x60000215c2d0。我很想知道我应该怎么做才能解决这个问题:-)
    【解决方案2】:

    好的,找到了。

    第一步是使用NSTableColumn.sortDescriptorPrototype在列上设置自定义NSSortDescriptor

    要创建 NSSortDescriptor,您可以使用: init(key: String?, ascending: Bool, comparator: Comparator) 或者 init(key: String?, ascending: Bool, selector: Selector?)

    这将给比较器:

    column.sortDescriptorPrototype = NSSortDescriptor(key: "myBindingKeyPath", ascending: true, comparator: { (obj1, obj2) -> ComparisonResult in
    // .... your logic     
        return .orderedSame
    })
    

    对于选择器:

    Column.sortDescriptorPrototype = NSSortDescriptor(key: "myBindingKeyPath", ascending: true, selector: #selector(MyBinary.compare(_:)))
            }
    

    以及通过选择器访问的 MyBinary 类中的比较函数:

    @objc
        public func compare(_ other: PDFBinary) -> ComparisonResult {
            return .orderedSame
        }
    

    第二个解决方案解决了我的问题,因为我使用了autosaveTableColumns 属性,这会导致第一个解决方案出现问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-24
      • 2013-06-25
      • 1970-01-01
      • 2012-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-11
      相关资源
      最近更新 更多