排序NSTableView [英] Sorting NSTableView

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

问题描述

我在NSTableView中有两个列,分别是Name和Salary,值分别为5-10.单击两个列的标题后,我想对这些列进行排序. Internet上存在大量数据,但我无法使用这些数据.请帮我做可可粉.

I have two coloumn in NSTableView as Name and Salary with 5-10 values. 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.

推荐答案

每个表列都有一个方法setSortDescriptorPrototype 排序描述符是告诉数组如何对其自身进行排序(升序,降序,忽略大小写等)的方式. 遍历您希望可排序的每个列,并在每个列上调用此方法,然后传递所需的排序描述符(在我的情况下,我将使用列标识符)

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)

for (NSTableColumn *tableColumn in tableView.tableColumns ) {

    NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:tableColumn.identifier ascending:YES selector:@selector(compare:)];
    [tableColumn setSortDescriptorPrototype:sortDescriptor];
}

编写完这段初始化代码后,NSTableViewDataSource有一个方法- (void)tableView:(NSTableView *)aTableView sortDescriptorsDidChange:(NSArray *)oldDescriptors,该方法在更改排序描述符时会通知您,在数据源中实现此方法并将消息发送到数据数组以对其进行自我排序

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

- (void)tableView:(NSTableView *)aTableView sortDescriptorsDidChange:(NSArray *)oldDescriptors
{
    self.data = [self.data sortedArrayUsingDescriptors:sortDescriptors];
    [aTableView reloadData];
}

每次单击列标题时都会触发此方法,并且NSTableColumn会显示一个漂亮的小三角形,显示排序顺序.

This method will get fired each time a column header is clicked, and NSTableColumn shows a nice little triangle showing the sorting order.

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

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