设置和维护 NSTableView 的第一个 SortDescriptor
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,它“正常工作”的原因是因为表列在其绑定的 NSArrayController 上调用
setSortDescriptors:
。假设你希望表格保持可排序,但你总是想按“未读”排序,这就是我的做法:
首先,子类 NSArrayController 并覆盖
arrangeObjects
:这会将未读消息放在“顶部”(数组的开头)。我不确定这是最有效的排序算法,但我相信这是正确的方法。
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
: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.
我认为您可以在 awakeFromNib 期间在数组控制器上设置一个 sortDescriptors 数组。无需强制
arrangeObjects:
,此功能完全内置。当您单击列标题时,列仍将保持可排序。
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.The columns will still remain sortable when you click the column header.