简体   繁体   中英

Cocoa bindings - Sorting error for a specific column

I use a NSOutlineView bound to a NSTreeController. I successfully managed to sort all columns of the view, except one! This column displays a button that is enabled if the binaryData field is not nil. The binaryData field in the model is a relationship to a MyBinary NSManagedObject subclass which has a NSData? field. I use this method (a relationship) as recommended to avoid loading all NSData in memory even when not necessary.

I want this column to be sortable, and, when clicked, have all enabled buttons regrouped (ascending or descending order), and all disabled buttons regrouped.

In IB, like other columns, I bound the column value to:

  • Controller Key = arrangedObjects
  • Model Key Path = binaryData

But when I click on the column, I have the following stack:

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
)

How can I solve this problem? I thought of using Custom Sort Descriptors but I don't exactly know how to tell the OutlineView or the TreeController to use a specific descriptor for a specific column.

Thanks for your help!

how to tell the OutlineView or the TreeController to use a specific descriptor for a specific column

Set sortDescriptorPrototype of the table column.

OK, found it.

The first step was to set a custom NSSortDescriptor on the column using NSTableColumn.sortDescriptorPrototype .

To create the NSSortDescriptor you can use: init(key: String?, ascending: Bool, comparator: Comparator) or init(key: String?, ascending: Bool, selector: Selector?)

which would give for the comparator:

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

and for the selector:

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

and the compare function in the MyBinary class to be accessed via the selector:

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

The second solution solved my problem, because I was using the autosaveTableColumns property which would cause issue with the first solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM