如何让 NSFormatter 子类与 NSTableColumn 排序键和选择器一起使用?
我的设置:
- 我有一个 sqlite 数据库,从中填充 NSDictionary 对象的 NSMutableArray,这是我的 NSTableView 的数据源。
- 其中一列保存“时间”,时间是保存秒数的浮点数。
- 我想将此列中的值显示为分钟:秒。例如,我的数据是 123.4329387,我想显示 2:03,将 NSFormatter(或 NSNumberFormatter)的子类应用于列中的 NSTextField 时,我没有问题。
- 我通过使用 IB 中的表列属性设置了排序,我只需将排序键设置为“时间”,将选择器设置为“比较:”,无需格式化程序即可正常工作。
目前,当我排序(降序)
1:37、1:31、0:10、0:10、0:09、1:30、1:30、1:26、0:09
等 时,这给了我类似的结果废话,看起来好像发生了什么事,但绝对没有解决。
如何让排序查看基础数据而不是格式化值?或者,如何指定自定义排序方法以及将所述方法的代码放在哪里?我已经搜索了很多,但没有找到任何可以帮助我解决这个问题的东西,非常感谢任何对此的帮助。
My setup:
- I have a sqlite database from which I populate a NSMutableArray of NSDictionary objects this is the DataSource for my NSTableView.
- One of the columns holds "time", the time is a float that holds seconds.
- I would like to display the values in this column as minutes:seconds. For instance my data would be 123.4329387 I want to display 2:03 which I have no problem doing with a subclass of NSFormatter (or NSNumberFormatter) applied to my NSTextField in the column.
- I have sorting set up by using the Table Column Attributes in IB, I just have the sort key set to "time" and the selector set to "compare:" which works fine without the formatter.
Currently this gives me something like this when I sort (descending)
1:37, 1:31, 0:10, 0:10, 0:09, 1:30, 1:30, 1:26, 0:09
and similar nonsense, it looks like something is going on but it's definitely not sorted.
How do I get the sort to look at the underlying data instead of the formatted value? Alternately, how do I specify a custom sort method and where do I put the code for said method? I have searched around quite a bit and have not found anything to help me out with this problem, any help with this is most appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事实证明,当它进行排序时,它使用的是 NSString 类中的 Compare: ,但它使用的是底层数字数据。我不太清楚这里到底发生了什么,但我设法通过在名为“timeCompare”的 NSString 类别中创建自己的比较方法来解决这个问题。然后在界面生成器中,我只需将“timeCompare:”放入表列的选择器字段中。这让它调用我的自定义比较方法,我刚刚从该方法转换了给定 NSNumbers 的字符串并在它们上调用 Compare: 。
It turns out that when it was sorting it was using compare: from the NSString class but it was using the underlying numeric data. I'm not exactly sure what all is going on under the hood here but I managed to fix this by creating my own compare method in a category of NSString called "timeCompare". Then in interface builder I just put "timeCompare:" in the selector field for the the table column. This gets it to call my custom compare method from which I just converted the strings given to NSNumbers and call compare: on them.