设置和维护 NSTableView 的第一个 SortDescriptor

发布于 2024-12-19 22:23:09 字数 522 浏览 0 评论 0原文

我有一个 NSTableView,其列绑定到 NSArrayController。

表格视图显示电子邮件列表:

  • 如果未读则标记
  • 主题
  • 附件大小

用户可以单击“附件大小”列对列表进行排序,但我希望表格始终首先按“未读”标记排序 以便未读消息始终保留在列表顶部。

我没有将数组控制器的排序描述符绑定到任何东西,但是通过单击表列,表排序可以神奇地工作(为什么?)。有什么方法可以拦截设置数组控制器的排序描述符并首先插入“未读”排序描述符?

按附件大小排序的表格示例:

UNREAD▼ SUBJECT ATTACHMENT SIZE▼
------  ------- ------------------
  yes   Hello.. 110kb
  yes   Test...  90kb

  no    Foobar  200kb
  no    Hey     100kb
  no    Test2    10kb

I have an NSTableView with columns bound to an NSArrayController.

The table view shows a list of email messages:

  • Flag if unread
  • Subject
  • Attachment size

The user can click on the Attachment Size column to sort the list, but I would like the table to always be sorted by the "unread" flag first so that the unread messages always remain at the top of the list.

I did not bind the Array Controller's sort descriptors to anything, yet table sorting works magically by clicking on the table columns (why?). Is there some way I can intercept setting the Array Controller's sort descriptors and insert the "Unread" sort descriptor first?

Example of a table sorted by attachment size:

UNREAD▼ SUBJECT ATTACHMENT SIZE▼
------  ------- ------------------
  yes   Hello.. 110kb
  yes   Test...  90kb

  no    Foobar  200kb
  no    Hey     100kb
  no    Test2    10kb

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

横笛休吹塞上声 2024-12-26 22:23:09

好吧,它“正常工作”的原因是因为表列在其绑定的 NSArrayController 上调用 setSortDescriptors:

假设你希望表格保持可排序,但你总是想按“未读”排序,这就是我的做法:

首先,子类 NSArrayController 并覆盖 arrangeObjects

- (NSArray *)arrangeObjects:(NSArray *)objects {

    NSMutableArray *oldSorted = [[super arrangeObjects:objects] mutableCopy];
    NSMutableArray *newSorted = [NSMutableArray arrayWithCapacity:[oldSorted count]];

    for (id anObject in oldSorted)
        if ([[anObject valueForKey:@"isUnread"] boolValue])
            [newSorted addObject:anObject];

    [oldSorted removeObjectsInArray:newSorted];
    [newSorted addObjectsFromArray:oldSorted];
    [oldSorted release];

    return newSorted;
}

这会将未读消息放在“顶部”(数组的开头)。我不确定这是最有效的排序算法,但我相信这是正确的方法。

Well, the reason it "just works" is because the table columns call setSortDescriptors: on their bound NSArrayController.

Assuming you want the table to remain sortable, but you always want to sort by "unread", this is how I would go about it:

First, subclass NSArrayController and override arrangeObjects:

- (NSArray *)arrangeObjects:(NSArray *)objects {

    NSMutableArray *oldSorted = [[super arrangeObjects:objects] mutableCopy];
    NSMutableArray *newSorted = [NSMutableArray arrayWithCapacity:[oldSorted count]];

    for (id anObject in oldSorted)
        if ([[anObject valueForKey:@"isUnread"] boolValue])
            [newSorted addObject:anObject];

    [oldSorted removeObjectsInArray:newSorted];
    [newSorted addObjectsFromArray:oldSorted];
    [oldSorted release];

    return newSorted;
}

This puts unread messages at the "top" (beginning of array). I'm not sure this is the most efficient sorting algorithm, but I believe it's the correct way to go about it.

一页 2024-12-26 22:23:09

我认为您可以在 awakeFromNib 期间在数组控制器上设置一个 sortDescriptors 数组。无需强制 arrangeObjects:,此功能完全内置。

- (void)awakeFromNib {
    [super awakeFromNib];
    NSSortDescriptor *unreadSorter = [[NSSortDescriptor sortDescriptorWithKey:@"isUnread" ascending:NO)] autorelease];
    NSArray *arrayOfSortDescriptors = [NSArray arrayWithObject:unreadSorter];
    [self setSortDescriptors:arrayOfSortDescriptors];
}

当您单击列标题时,列仍将保持可排序。

I think that you can just set an array of sortDescriptors on your array controller during awakeFromNib. No need to force arrangeObjects:, this functionality totally built in.

- (void)awakeFromNib {
    [super awakeFromNib];
    NSSortDescriptor *unreadSorter = [[NSSortDescriptor sortDescriptorWithKey:@"isUnread" ascending:NO)] autorelease];
    NSArray *arrayOfSortDescriptors = [NSArray arrayWithObject:unreadSorter];
    [self setSortDescriptors:arrayOfSortDescriptors];
}

The columns will still remain sortable when you click the column header.

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