Sort the contents of an NSScrollView, NSTableView,

2019-02-20 23:52发布

问题:

I have an NSScrollView wich is bound with an ArrayController. I need to sort the content alphabetically. I've tried to do this with bindings but I can't find the right thing to bind. I used the following sortDescriptor on my ArrayController.

[myArrayController setSortDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"myKey" ascending:YES selector:@selector(compare:)]]];

Do I miss a step in the process or am I not even close to sorting the content?

回答1:

Did you try FilterPredicate ? this is a sample code:

NSDate *afterDate=[balanceDateAfter objectValue];
NSDate *beforeDate=[balanceDateBefore  objectValue];
NSComparisonResult duration=[beforeDate timeIntervalSinceDate:afterDate];

duration=duration/3600;
duration=duration/24;

[checkInBalanceArray setFilterPredicate:[NSPredicate predicateWithFormat:@"(checkDate >= %@) AND (checkDate <= %@)", afterDate, beforeDate]];

[checkOutBalanceArray setFilterPredicate:[NSPredicate predicateWithFormat:@"(checkDate >= %@) AND (checkDate <= %@)", afterDate, beforeDate]];