Search code examples
swiftcocoansoutlineview

NSOutlineView column doesn't sort


I can't sort a NSOutlineView in Mac OS X 10.10...

    let tableColumn = outlineView.tableColumnWithIdentifier("LocationName")
    println(tableColumn?.description)
    let sorter = NSSortDescriptor(key: "self", ascending: false)
    tableColumn!.sortDescriptorPrototype = sorter
    outlineView.reloadData()

neither with the code above neither setting sorting attributes in Interface Builder.

Maybe something wrong with "self"?

NSOutlineView column contains swift strings, compare: should work, right? Maybe I simply have to write a comparer?


Solution

  • Setting a table column's sortDescriptorPrototype doesn't cause it to sort. It just determines what order that column would be in if the outline view were sorted by that column.

    To sort an outline view, you set the outline view's sortDescriptors property. However, if you're using a data source (rather than bindings) to provide the outline's data, the outline view is just going to tell your data source to sort its data by calling its -outlineView:sortDescriptorsDidChange: method. It is then your data source's responsibility to actually sort its data so that subsequent queries by the outline view get the data in sorted order. Your data source is also responsible for adjusting the outline view's selection based on how items moved around in the order.