Macos 使用多个排序描述符对基于视图的tableview进行排序不会';行不通
对于使用多个排序描述符的CoreData应用程序,我无法在基于视图的tableview上进行排序。 我有两列,第一列有“bank”和“account昵称”作为值,第二列有“bankAccount”。Macos 使用多个排序描述符对基于视图的tableview进行排序不会';行不通,macos,sorting,nstableview,Macos,Sorting,Nstableview,对于使用多个排序描述符的CoreData应用程序,我无法在基于视图的tableview上进行排序。 我有两列,第一列有“bank”和“account昵称”作为值,第二列有“bankAccount”。 我想按“银行”再按“账户昵称”再按“银行账户”排序。如果我单击第一列。 我想按“银行账户”,然后是“银行”,然后是“账户昵称”进行排序。 如果我单击第二列。 创建SortDescriptor数组并将其绑定到我的arraycontroller不起作用: sortForStockInfo = [ NSA
我想按“银行”再按“账户昵称”再按“银行账户”排序。如果我单击第一列。
我想按“银行账户”,然后是“银行”,然后是“账户昵称”进行排序。
如果我单击第二列。
创建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];
tableview将“排序描述符”绑定到阵列控制器的“排序描述符”。我想,这就是我所要做的。但它不起作用。我错过了什么
奇怪的是:如果我使用相同的方法,但我填充了Sortkey、Selector和Order的属性列,那么它只针对一个方面进行排序(例如,bank或account.account昵称保持未排序)。因为我只能为每列定义一个条件
sortDescriptors
是一个数组,单击列的sortDescriptor插入索引0处。当用户单击第0列,然后单击第1列时,排序顺序为第1列,第0列。
实现委托方法-(void)tableView:(NSTableView*)tableView单击tableColumn:(NSTableColumn*)tableColumn
,并设置arraycontroller的排序描述符
。例如:
- (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:)]];
}
}
}
sortDescriptors
是一个数组,单击列的sortDescriptor插入索引0。当用户单击第0列,然后单击第1列时,排序顺序为第1列,第0列。
实现委托方法-(void)tableView:(NSTableView*)tableView单击tableColumn:(NSTableColumn*)tableColumn
,并设置arraycontroller的排序描述符
。例如:
- (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:)]];
}
}
}