对 NSArrayController 支持的 NSTableView 进行排序
我有一个 NSArrayController 和一个 NSTableView。他们显示来自 iTunes 的曲目。我可以通过单击标题对列表进行排序。
有没有办法为表视图设置默认排序描述符,以便每次用户启动应用程序时它都会对相册进行排序?
我尝试在数组控制器和表视图上设置 sortDescriptor,但这没有任何改变。
谢谢
编辑:答案是正确的。但它需要一个 NSArray:
- (NSArray *)mainSortDescriptor {
return [NSArray arrayWithObjects:
[NSSortDescriptor sortDescriptorWithKey:@"album" ascending:YES],
[NSSortDescriptor sortDescriptorWithKey:@"trackNumber" ascending:YES],
nil];
}
I have a NSArrayController and a NSTableView. They show tracks from iTunes. I can sort the list by clicking in the header.
Is there a way to set up a default sort descriptor for the table view so it sorts for albums every time the user launches the app?
I tried to set the sortDescriptor on the array controller and the table view but that changes nothing.
Thank you
Edit: The answer is right. But it needs a NSArray:
- (NSArray *)mainSortDescriptor {
return [NSArray arrayWithObjects:
[NSSortDescriptor sortDescriptorWithKey:@"album" ascending:YES],
[NSSortDescriptor sortDescriptorWithKey:@"trackNumber" ascending:YES],
nil];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

如果您想绑定数组控制器的排序描述符,则必须将其绑定到某些东西。您可以将其放入应用程序委托中,例如:
然后您可以将 IB 中的绑定设置为
EDITED。我忘记了,当从 PyObjC 翻译这个时,我正在返回一个列表。哎呀。
If you want to bind the array controller's sort descriptor, you have to bind it to something. You can put this in your application delegate, for example:
Then you can set up the binding in IB as
EDITED. I forgot, when translating this from PyObjC, that I was returning a list. Oops.