使用多个排序描述符对基于视图的表视图进行排序不起作用 [英] Sorting a viewbased tableview with multiple sort descriptors doesn't work
问题描述
我无法使用多个排序描述符在CoreData App的基于视图的表视图上进行排序.
我有2列,第一列具有"bank"和"accountNickName"作为值,第二列具有"bankAccount".
我想按银行",然后按"accountNickName",然后按"bankAccount"进行排序.如果我点击第一栏.
我想按"bankAccount"排序,然后按"bank"排序,然后按"accountNickName"排序.
如果我单击第二列.
创建sortDescriptor数组并将其绑定到我的arraycontroller不起作用:
I cannot get sorting working on a view based tableview for a CoreData App using multiple sort descriptors.
I have 2 columns, my first column has "bank" and "accountNickName" as values , my second column has "bankAccount".
I want to sort by "bank" then "accountNickName" and then by "bankAccount". If I click on the 1st column.
I want to sort by "bankAccount", then "bank", then "accountNickName".
If I click on the second column.
Creating an array of sortDescriptors and bind this to my arraycontroller doesn't work:
sortForStockInfo = [ NSArray arrayWithObjects:
[NSSortDescriptor sortDescriptorWithKey:@"bank" ascending:YES selector:@selector(compare:)],
[NSSortDescriptor sortDescriptorWithKey:@"accountNickName" ascending:YES selector:@selector(compare:)],
[NSSortDescriptor sortDescriptorWithKey:@"account" ascending:YES selector:@selector(compare:)],
nil];
tableview的排序描述符"绑定到阵列控制器的排序描述符".我以为,这就是我要做的.但这是行不通的.我错过了什么?
The tableview has "Sort Descriptors" bound to the Array Controller's "Sort Descriptors". I thought, this is all I had to do. But it doesn't work. What have I missed out ?
足够奇怪:如果我使用相同的方法,但是我填写了Sortkey,Selector和Order的column属性,则它仅针对一个方面进行排序(例如,银行或帐户.accountNickName仍未排序).因为我只能为每列定义一个条件.
Strange enough: If I use the same approach, but I fill the columns attributes for Sortkey, Selector and Order, it is sorting only for one aspect (e.g. bank or account. accountNickName remains unsorted ) . Because I can only define one criteria per column.
推荐答案
sortDescriptors
是一个数组,被单击列的sortDescriptor插入索引0.当用户单击列0,然后单击column1时,排序顺序是column1,column0.
实现委托方法- (void)tableView:(NSTableView *)tableView didClickTableColumn:(NSTableColumn *)tableColumn
并设置arraycontroller的sortDescriptors
.示例:
sortDescriptors
is an array, the sortDescriptor of the clicked column is inserted at index 0. When the user clicks on column 0 and then on column1, the sort order is column1, column0.
Implement delegate method - (void)tableView:(NSTableView *)tableView didClickTableColumn:(NSTableColumn *)tableColumn
and set sortDescriptors
of the arraycontroller. Example:
- (void)tableView:(NSTableView *)tableView didClickTableColumn:(NSTableColumn *)tableColumn {
NSArray *sortDescriptors = self.arrayController.sortDescriptors;
if (sortDescriptors && [sortDescriptors count] > 0) {
NSSortDescriptor *firstSortDescriptor = sortDescriptors[0]; // sort descriptor of the clicked column
BOOL ascending = firstSortDescriptor.ascending;
if ([firstSortDescriptor.key isEqualToString:@"bank"]) {
self.arrayController.sortDescriptors = @[
[NSSortDescriptor sortDescriptorWithKey:@"bank" ascending:ascending selector:@selector(compare:)],
[NSSortDescriptor sortDescriptorWithKey:@"accountNickName" ascending:ascending selector:@selector(compare:)],
[NSSortDescriptor sortDescriptorWithKey:@"account" ascending:ascending selector:@selector(compare:)]];
}
else
if ([firstSortDescriptor.key isEqualToString:@"account"]) {
self.arrayController.sortDescriptors = @[
[NSSortDescriptor sortDescriptorWithKey:@"account" ascending:ascending selector:@selector(compare:)],
[NSSortDescriptor sortDescriptorWithKey:@"bank" ascending:ascending selector:@selector(compare:)],
[NSSortDescriptor sortDescriptorWithKey:@"accountNickName" ascending:ascending selector:@selector(compare:)]];
}
}
}
这篇关于使用多个排序描述符对基于视图的表视图进行排序不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!