[英]Sorting NSTableView
I have two coloumn in NSTableView as Name and Salary with 5-10 values.我在 NSTableView 中有两列作为 Name 和 Salary ,有 5-10 个值。 I want to sort these coloumn after click on header of both the column.
单击两列的标题后,我想对这些列进行排序。 There is lots of data present in Internet but I am not able to use these.
互联网上存在大量数据,但我无法使用这些数据。 Please help me to do this in cocoa.
请帮我在可可中做到这一点。
Thanks in advance and appreciate any help.提前致谢并感谢任何帮助。
Each table column has a method setSortDescriptorPrototype
Sort descriptors are ways of telling the array how to sort itself (ascending, descending, ignoring case etc.) Iterate over each of the columns you want as sortable and call this method on each of those columns, and pass the required sort descriptor (In my case I'll be using the column identifier)每个表列都有一个方法
setSortDescriptorPrototype
排序描述符是告诉数组如何对自身进行排序(升序、降序、忽略大小写等)的方法。迭代你想要排序的每一列,并在每一列上调用这个方法,以及传递所需的排序描述符(在我的情况下,我将使用列标识符)
for (NSTableColumn *tableColumn in tableView.tableColumns ) {
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:tableColumn.identifier ascending:YES selector:@selector(compare:)];
[tableColumn setSortDescriptorPrototype:sortDescriptor];
}
After writing this piece of initialization code, NSTableViewDataSource
has a method - (void)tableView:(NSTableView *)aTableView sortDescriptorsDidChange:(NSArray *)oldDescriptors
that notifies you whenever a sort descriptor is changed, implement this method in the data source and send a message to the data array to sort itself写完这段初始化代码后,
NSTableViewDataSource
有一个方法- (void)tableView:(NSTableView *)aTableView sortDescriptorsDidChange:(NSArray *)oldDescriptors
,它会在排序描述符发生变化时通知你,在数据源中实现这个方法并发送一个向数据数组发送消息以对自身进行排序
- (void)tableView:(NSTableView *)aTableView sortDescriptorsDidChange:(NSArray *)oldDescriptors
{
self.data = [self.data sortedArrayUsingDescriptors:sortDescriptors];
[aTableView reloadData];
}
This method will get fired each time a column header is clicked, and NSTableColumn
shows a nice little triangle showing the sorting order.每次单击列标题时都会触发此方法,并且
NSTableColumn
显示一个显示排序顺序的漂亮小三角形。
I stumbled upon this question while looking for the easiest way of implementing something similar.我在寻找实现类似功能的最简单方法时偶然发现了这个问题。 Although the original question is old, I hope someone finds my answer useful!
虽然原来的问题很老,但我希望有人觉得我的回答有用! Please note that I am using Xcode 5.1.1
请注意,我使用的是 Xcode 5.1.1
Ok so to do this you need to:好的,要做到这一点,您需要:
Based on the Table View Programming Guide for Mac.
基于 Mac 的 Table View Programming Guide。 The compare: method works with NSString, NSDate, and NSNumber objects.
compare: 方法适用于 NSString、NSDate 和 NSNumber 对象。 If your table column contains only strings, you may want to consider using the caseInsensitiveCompare: method if case sensitivity is unimportant.
如果您的表列仅包含字符串,并且区分大小写不重要,您可能需要考虑使用 caseInsensitiveCompare: 方法。 However, consider replacing these method signatures with the localizedCompare: or localizedCaseInsensitiveCompare: methods to take into the account the user's language requirements.
但是,考虑将这些方法签名替换为 localizedCompare: 或 localizedCaseInsensitiveCompare: 方法以考虑用户的语言要求。
Finally, you need to declare the tableView:sortDescriptorsDidChange: method in your Table View Controller in the format shown below:最后,您需要在 Table View Controller 中以如下所示的格式声明 tableView:sortDescriptorsDidChange: 方法:
-(void)tableView:(NSTableView *)mtableView sortDescriptorsDidChange:(NSArray *)oldDescriptors
{
[listArray sortUsingDescriptors: [mtableView sortDescriptors]];
[tableView reloadData];
}
Just had lately the same issue to get tableView sorted.最近刚刚遇到了同样的问题来对 tableView 进行排序。 My approach :
我的方法:
Worked perfect for me.非常适合我。 No need to set prototypes for columns or something else.
无需为列或其他东西设置原型。
Thanks very much ,It is usefullly for my question.非常感谢,这对我的问题很有用。 my code like this
我的代码是这样的
First, set unique values in the XIB interface,like name...首先,在XIB界面设置唯一值,比如name...
- (void)viewDidLoad {
[super viewDidLoad];
self.itemTableView.dataSource = self;
self.itemTableView.delegate = self;
self.itemTableView.selectionHighlightStyle = NSTableViewSelectionHighlightStyleRegular;
self.itemTableView.usesAlternatingRowBackgroundColors = YES;
for (NSTableColumn *tableColumn in self.itemTableView.tableColumns ) {
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:tableColumn.identifier ascending:NO selector:@selector(compare:)];
[tableColumn setSortDescriptorPrototype:sortDescriptor];
}
}
-(void)tableView:(NSTableView *)tableView sortDescriptorsDidChange:(NSArray<NSSortDescriptor *> *)oldDescriptors{
NSLog(@"sortDescriptorsDidChange:%@",oldDescriptors);
[self.itemArr sortUsingDescriptors:[tableView sortDescriptors]];
[self.itemTableView reloadData];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.