简体   繁体   English

排序NSTableColumn内容

[英]Sorting NSTableColumn contents

I have a problem with sorting NSTableColumn contents. 我有一个排序NSTableColumn内容的问题。 In my NSTableView there are three columns: File, Size, Path. 在我的NSTableView中有三列:文件,大小,路径。 The contents are stored in NSMutableArray. 内容存储在NSMutableArray中。 Each object in this array is a NSDictionary containing three keys: file, size and path - value for each is a NSString. 此数组中的每个对象都是一个包含三个键的NSDictionary:文件,大小和路径 - 每个键的值都是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. 在Interface Builder中,在每个Table Column的属性中,我可以选择排序选项:Selector:IB输入“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. 所以我找到了完整的解决方案。

First, go to Interface Builder. 首先,转到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. 其中'file'是字典的关键字,内容显示在列中。

Selector: compare:

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. 现在,保存所有更改并跳转到Xcode,其中的类是模型,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

these two are needed to conform the NSTableDataSource informal protocol. 这两个是符合NSTableDataSource非正式协议所必需的。 You can read about it at the MacDev Center. 您可以在MacDev中心阅读相关内容。

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]; 
}

And that's all. 就这样。 It's working in my app, hope it will work in your case :D Everything should work automatically. 它在我的应用程序中工作,希望它适用于您的情况:D一切都应该自动工作。 I've just saved all files and Built the app. 我刚刚保存了所有文件并构建了应用程序。 And it worked. 它奏效了。 :) :)

Site that helped me: CocoaDev: SortingNSTableView 帮助我的网站: CocoaDev:SortingNSTableView

Key is the key that you use in dictionary to retrieve the value. 重点是key ,你在使用字典检索值。

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 put value.file into the Sort Key field in IB. 如果你的密钥是@“file”,@“size”,@“path”,你想对file进行排序,那么尝试将value.file放入IB中的Sort Key字段。

Use keys that you use when inserting values into your NSDictionary . 使用在NSDictionary插入values时使用的键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM