使用多个排序描述符对基于视图的表视图进行排序不起作用



我无法使用多个排序描述符对CoreData应用程序的基于视图的表视图进行排序。我有两列,第一列有"bank"one_answers"accountNickName"作为值,第二列有"bankAccount">
我想先按"银行",然后按"accountNickName",再按"bankAccount"排序。如果我点击第一列
我想按"bankAccount"、"bank"one_answers"accountNickName"进行排序
如果我点击第二列
创建一个sortDescriptor数组并将其绑定到我的arraycontroller不起作用:

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];

表视图具有绑定到阵列控制器的"排序描述符"的"排序描述器"。我想,这就是我所要做的。但它不起作用。我错过了什么?

奇怪的是:如果我使用相同的方法,但我填充了Sortkey、Selector和Order的列属性,那么它只对一个方面进行排序(例如银行或账户。accountNickName仍然未排序)。因为我每列只能定义一个条件。

sortDescriptors是一个数组,点击列的sortDDescriptor插入索引0。当用户单击第0列,然后单击第1列时,排序顺序为第1列,第0列。实现委托方法- (void)tableView:(NSTableView *)tableView didClickTableColumn:(NSTableColumn *)tableColumn,设置阵列控制器的sortDescriptors。示例:

- (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:)]];
}
}
}

最新更新