对 NSArrayController 支持的 NSTableView 进行排序

发布于 10-28 14:09 字数 518 浏览 7 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

抱猫软卧2024-11-04 14:09:31

如果您想绑定数组控制器的排序描述符,则必须将其绑定到某些东西。您可以将其放入应用程序委托中,例如:

- (NSArray *)tracksSortDescriptors {
    return [NSArray arrayWithObject:
             [NSSortDescriptor sortDescriptorWithKey:@"albumName"
                                           ascending:YES]];
}

然后您可以将 IB 中的绑定设置为

Bind to: MyAppDelegate  
Model Key Path: tracksSortDescriptors

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:

- (NSArray *)tracksSortDescriptors {
    return [NSArray arrayWithObject:
             [NSSortDescriptor sortDescriptorWithKey:@"albumName"
                                           ascending:YES]];
}

Then you can set up the binding in IB as

Bind to: MyAppDelegate  
Model Key Path: tracksSortDescriptors

EDITED. I forgot, when translating this from PyObjC, that I was returning a list. Oops.

撩发小公举2024-11-04 14:09:31

我尝试了这个,但不太有效 - 在每个应用程序启动时求助,但不在应用程序运行时求助。

最终,我注意到在我的 NSArrayController 对象中,以下框未被选中(啊!):

“自动重新排列内容”

...所以,仅供有相同问题的人参考:确保该框处于打开状态:)

I tried this, didn't quite work - resorted on each app start, but not while the app was running.

Eventually, I noticed that in my NSArrayController object, the following box was unticked (argh!):

"Auto rearrange content"

...so, FYI to anyone who has the same problem: make sure that box is ON :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文