对 NSTableColumn 内容进行排序
我在对 NSTableColumn 内容进行排序时遇到问题。在我的 NSTableView 中有三列:文件、大小、路径。内容存储在 NSMutableArray 中。该数组中的每个对象都是一个 NSDictionary,包含三个键:文件、大小和路径 - 每个值都是一个 NSString。
在 Interface Builder 中,在每个表列的属性中,我可以选择排序选项: 选择器:IB 输入了“compare:”,我认为这是可以的,因为我比较 NSStrings。 排序键 - 这就是我认为的问题 - 我不知道在这里输入什么。
有什么线索吗?如果您对我的代码有疑问,请询问。
I have a problem with sorting NSTableColumn contents. In my NSTableView there are three columns: File, Size, Path. The contents are stored in NSMutableArray. Each object in this array is a NSDictionary containing three keys: file, size and path - value for each is a NSString.
In Interface Builder, in each Table Column's attributes I can choose sorting options:
Selector: IB entered "compare:" which I think is ok, because I compare NSStrings.
Sort Key - and that's the problem I think - I don't know what to enter here.
Any clues? If you've got questions about my code, please ask.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
所以我找到了完整的解决方案。
首先,转到界面生成器。选择要排序的列。转到列的检查器并选择第一个按钮以查看“表列属性”。设置适当的值(从字面上看,不需要“或'或@):
其中'file'是字典的键,内容显示在您的列中。
标准排序功能。
现在,在此处保存所有更改并跳转到Xcode,其中的类是 NSTableView 中显示的数据源。您应该已经知道您需要两个方法:
两个方法需要符合 NSTableDataSource 非正式协议。
这 你要做的就是添加一个新方法:
它可以包含一个非常简单的代码来完成这件事:
这就是它在我的应用程序中的工作,希望它能在你的情况下工作:D 一切都应该自动工作。我刚刚保存了所有文件并构建了应用程序,它成功了:)
对我有帮助的网站:
CocoaDev:排序NSTableView
So I found the complete solution.
First, go to Interface Builder. Select column that you want to sort. Go to the column's inspector and choose the first button to see the "Table Column Attributes". Set appropriate values (literally, no " or ' or @ are needed):
where 'file' is the key of dictionary that contents is shown in your column.
standard sort function.
Now, save all the changes here and jump to Xcode, to the class in which is the model, source of the data shown in NSTableView. You should already know that you need two methods there:
these two are needed to conform the NSTableDataSource informal protocol. You can read about it at the MacDev Center.
Now all you have to do is to add a new method:
it can contain a very simple code that will do the thing:
And that's all. It's working in my app, hope it will work in your case :D Everything should work automatically. I've just saved all files and Built the app. And it worked. :)
Site that helped me:
CocoaDev: SortingNSTableView
Key 是您在字典中用于检索值的
键
。在您的情况下,您有三个键:文件、大小和路径。 选择要排序的记录。 该键用于从每条记录中检索用于排序的值。
如果您的键是 @"file"、@"size"、@"path" 并且您想对
file
进行排序,请尝试将value.file
放入Sort Key
字段。使用将
值
插入NSDictionary
时使用的键。Key is the
key
that you use in dictionary to retrieve the value.In your case you have three keys: file, size and path. Select the one on which you want to sort. The key is used to retrieve value from each record to be used for sorting.
If your keys are @"file", @"size", @"path" and you want to sort on
file
then try to putvalue.file
into theSort Key
field in IB.Use keys that you use when inserting
values
into yourNSDictionary
.