我有一个 NSArrayController 和一个 NSTableView。他们显示来自 iTunes 的轨道。我可以通过单击标题对列表进行排序。
有没有办法为 TableView 设置默认排序描述符,以便每次用户启动应用程序时它都会对相册进行排序?
我尝试在数组 Controller 和 TableView 上设置 sortDescriptor,但这没有任何改变。
谢谢
编辑:答案是正确的。但它需要一个 NSArray:
- (NSArray *)mainSortDescriptor {
return [NSArray arrayWithObjects:
[NSSortDescriptor sortDescriptorWithKey:@"album" ascending:YES],
[NSSortDescriptor sortDescriptorWithKey:@"trackNumber" ascending:YES],
nil];
}
最佳答案
如果你想绑定(bind)数组 Controller 的排序描述符,你必须将它绑定(bind)到某个东西。您可以将其放入应用程序委托(delegate)中,例如:
- (NSArray *)tracksSortDescriptors {
return [NSArray arrayWithObject:
[NSSortDescriptor sortDescriptorWithKey:@"albumName"
ascending:YES]];
}
然后你可以在IB中设置绑定(bind)为
Bind to: MyAppDelegate Model Key Path: tracksSortDescriptors
已编辑。我忘记了,当从 PyObjC 翻译这个时,我正在返回一个列表。哎呀。
关于cocoa - 对 NSArrayController 支持的 NSTableView 进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5478357/