排序NSTableColumn的内容 [英] Sorting NSTableColumn contents

查看:168
本文介绍了排序NSTableColumn的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对NSTableColumn内容排序有问题.在我的NSTableView中,有三列:文件",大小"和路径".内容存储在NSMutableArray中.该数组中的每个对象都是一个NSDictionary,其中包含三个键:文件,大小和路径-每个对象的值都是一个NSString.

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.

在Interface Builder中,在每个表列"的属性中,我可以选择排序选项:选择器:IB输入"compare:",我认为还可以,因为我比较了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.

推荐答案

所以我找到了完整的解决方案.

So I found the complete solution.

首先,转到Interface Builder.选择要排序的列.转到列的检查器,然后选择第一个按钮以查看表列属性".设置适当的值(从字面上讲,不需要,"或@):

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):

Sort key: file

其中文件"是字典的关键字,其内容显示在您的栏中.

where 'file' is the key of dictionary that contents is shown in your column.

Selector: compare:

标准排序功能.

现在,将所有更改保存在这里,并跳转到Xcode,跳转到其中是模型的类,即NSTableView中显示的数据源.您应该已经知道那里需要两种方法:

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:

-(NSInteger)numberOfRowsInTableView:(NSTableView *)tableView
-(id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn
 *)tableColumn row:(NSInteger)row

这两个必须符合NSTableDataSource非正式协议.您可以在MacDev中心阅读有关它的信息.

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:

-(void)tableView:(NSTableView *)tableView sortDescriptorsDidChange: (NSArray *)oldDescriptors

它可以包含执行该操作的非常简单的代码:

it can contain a very simple code that will do the thing:

-(void)tableView:(NSTableView *)tableView sortDescriptorsDidChange: (NSArray *)oldDescriptors 
{       
    NSArray *newDescriptors = [tableView sortDescriptors];
    [results sortUsingDescriptors:newDescriptors]; 
    //"results" is my NSMutableArray which is set to be the data source for the NSTableView object.
    [tableView reloadData]; 
}

仅此而已.它正在我的应用程序中运行,希望它能在您的情况下运行:D一切都应该自动运行.我刚刚保存了所有文件并构建了应用程序.而且有效.:)

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. :)

对我有帮助的网站: CocoaDev:SortingNSTableView

这篇关于排序NSTableColumn的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆