objective c - Sorting an NSTableColumn using transformed values -
my class has property enum
. property bound nstablecolumn
.
typedef enum _status { unknown=0, win, osx } status;
in order make sure nstablecolumn
shows proper string representation, instead of displaying numeric 0, 1, , 2, use nsvaluetransformer
.
so unknown
becomes "unknown os". win
becomes "ms windows" , osx
becomes "osx 10.8". table column displays these values correctly.
i need sort column using transformed values. if use
[nssortdescriptor sortdescriptorwithkey:@"status" ascending:yes selector:@selector(compare:)];
the column sorted in order:
unknown os
ms windows
osx 10.8
which make sense because 0 < 1 < 2. require sorted using string counterparts, make it:
ms windows
osx 10.8
unknown os
how can without changing order of enum?
you're using standard compare:
method in selector. it's comparing enums. create own method compares way want. can call nsvaluetransformer string values, , compare those, , return appropriate nscomparisonresult.
[edit] ok first suggestion won't work (without using categories). should. assuming myvaluetransformer instantiated, , status nsnumber:
sortdescriptor = [nssortdescriptor sortdescriptorwithkey:@"status" ascending: yes comparator:^(id first, id second){ nsnumber* firstnum = (nsnumber*)first; nsnumber* secondnum = (nsnumber*)second; if ([myvaluetransformer transformedvalue:firstnum] == [myvaluetransformer transformedvalue:secondnum]) { return nsorderedsame; } else if ([myvaluetransformer transformedvalue:firstnum] > [myvaluetransformer transformedvalue:secondnum]) { return nsordereddescending; } else { return nsorderedascending; } }];
Comments
Post a Comment