如何使用sortDescriptors在Swift中对NSTableView进行排序? [英] How to sort NSTableView in Swift using sortDescriptors?

查看:404
本文介绍了如何使用sortDescriptors在Swift中对NSTableView进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在用Swift编写的Cocoa应用程序中使用基于视图的NSTableView,并且希望在两个表列上实现排序功能.但是,在Objective-C中,您可以通过首先在Attribute Inspector中设置排序键"来实现它,然后实现名为tableView: sortDescriptorsDidChange:的数据源委托方法.

I use view-based NSTableView in my Cocoa app which is written in Swift, and want to implement a sort functionality on two table columns. However, in Objective-C, you can implement it by first setting the "Sort Key" in Attribute Inspector, and then implement the data source delegate method named tableView: sortDescriptorsDidChange:.

但是,此方法将sortDescriptor作为参数,并允许开发人员在方法中使用它,如下所示:

However, this method takes sortDescriptor as a parameter and lets developers use it within the method, like so:

- (void) tableView:( NSTableView *) tableView sortDescriptorsDidChange:( NSArray *) oldDescriptors { 
    [self.songs sortUsingDescriptors:tableView.sortDescriptors]; 
    [tableView reloadData]; 
}

但是,在Swift Array中,没有像sortUsingDescriptors这样的方法.因此,我首先尝试将Array转换为NSMutableArray以便使用NSMutableArray的方法,但是由于我的Swift数组被定义为AnyObject[],因此无法将其强制转换为NSMutableArray.

However, in Swift Array, there are no such method as sortUsingDescriptors. So I first tried to convert Array to NSMutableArray in order to use the NSMutableArray's method, but since my Swift Array is defined as AnyObject[], it cannot be casted to NSMutableArray.

那么我应该如何在Swift中对表视图实现排序功能?我知道Swift Array可以通过比较两个参数并返回bool值来使用sort函数对对象进行排序,但是可以使用sortDescriptors对表进行排序吗?还是应该忽略sortDescriptors参数,而是手动编写自己的排序逻辑? (但后来我不知道如何知道没有sortDescriptors值时单击的是哪一列).

So how should I implement the sort functionality to the table view in Swift? I know Swift Array can use sort function to sort the object by comparing the two arguments and returning bool values, but is it possible to use sortDescriptors to sort the table? Or should I just ignore the sortDescriptors argument and instead write my own sort logic manually? (but then I don't know how to tell what column is clicked without the sortDescriptors value).

推荐答案

可能最好的方法(至少现在)是首先将其转换为NSMutableArray,然后使用NSMutableArraysortUsingDescriptors方法对其进行排序,最后将其转换回原始数组,如下所示:

Probably the best way, at least right now, is to first convert it to NSMutableArray and then sort it using NSMutableArray's sortUsingDescriptors method, and finally convert it back to the original Array, like so:

func tableView(tableView: NSTableView!, sortDescriptorsDidChange oldDescriptors: [AnyObject]) {
    var songsAsMutableArray = NSMutableArray(array: songs)
    songsAsNSMutableArray.sortUsingDescriptors(tableView.sortDescriptors)
    songs = songsAsNSMutableArray
    tableView.reloadData()
}

顺便说一句,var songsAsMutableArray = songs as NSMutableArray会导致错误:NSArray is not a subtype of NSMutableArray,所以我创建了一个如上所示的NSMutableArray实例.

By the way, var songsAsMutableArray = songs as NSMutableArray causes an error: NSArray is not a subtype of NSMutableArray, so I created an NSMutableArray instance as shown above.

这篇关于如何使用sortDescriptors在Swift中对NSTableView进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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