Sorting in NSTableView
I am creating a NSTableView & using the delegate & datasource lin开发者_运维知识库k to the class I am flooding the table. In the class i am using the NSMutableArray to store the values for the table. can I do sorting in the table. How can I do that. I want to sort as per the values in the column.
I am using the sort hey & selector but when I run I am getting this error
[<NSCFString 0x18da00> valueForUndefinedKey:]: this class is not key value coding-compliant for the key name.
how to solve this.
You can approach this in one of two ways. The easier way, that's not as prone to failure if you aren't sure what you're doing, is to simply sort your content array. You can use NSMutableArray
's -sort...:
methods to sort it, and the results will appear sorted in the table.
However, the 'better' way is to have the table sort output on its own, using an NSSortDescriptor
(you can either set this in code, or through Interface Builder). To get a sort descriptor to work with custom classes, you'd have to make them KVC-compliant, but luckily, you're only working with NSString
s, so the work is already done for you. Either in Interface Builder, or in code, set the sort descriptor's key to self
(since you're comparing the strings themselves, and not their instance variables) and the selector to compare:
(since you just need to do a simple comparison, and NSString
s implement this method). That's easily done in Interface Builder (once you set self
as the key, compare:
will even pop up for you), though if you'd have wanted to make a complex sort descriptor, you'd have had to make it in code.
It looks like the values in your table are NSStrings
. You need to select something that NSString
responds to as the key. Probably the key self
will do the trick.
精彩评论