通过可可绑定在 IB 中完成时,按字符串长度对表列进行排序无法按预期工作
我制作了一个示例应用程序,使用 NSArrayController 通过 NSTableView 显示员工列表及其工资。
基本功能正在按预期工作。当在表列 - employeeName
的属性窗格中分配选择器 - compare
或 caseInsensitiveCompare
时,排序也可以正常工作。
当我尝试通过将 IB 中的选择器设置为 - length
(对于表列 - employeeName
)来按字符串长度对其进行排序时,出现问题。它确实按字符串长度排序,但这样做不恰当。
它正在对交替点击进行排序
即。第一次单击时,它会按长度降序对表中的内容进行排序。然后,在第二次单击时,它不会按长度升序排列内容,而是将箭头设置为升序。然后,在第三次单击时,它会按长度升序排列内容,但会在表格列顶部设置箭头,如降序排列等等...
任何人都可以建议我是否在某个地方错了?
谢谢,
米拉杰
I made a sample application displaying list of employees with their salaries over NSTableView using NSArrayController.
Basic functionality is working as intended. Also sorting is working fine when Selector - compare
or caseInsensitiveCompare
is assigned in attribute pane for table column- employeeName
.
Problem occurs when I try to sort it by string length by setting selector in IB as - length
, for the table column - employeeName
. It does sorting by string length but does it inappropriately.
It is doing sorting on alternate clicks
ie. on first click it sorts the content in table in descending order by length. Then on second click it does not arrange contents in ascending order by length but sets arrow on top as in ascending. Then on third click it arranges contents in ascending order by length but sets arrow on top of table column as in descending and so on...
Can anyone suggest if I am wrong somewhere?
Thanks,
Miraaj
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我猜你正在学习《Mac OS X 的 Cocoa 编程》第 8 章,
你需要将 sortKey 设置为
personName.length
并将选择器设置为compare:
对于专栏。然后排序描述符将通过“比较”字符串“长度”进行排序。
I'm going to guess you are working your way through Cocoa Programming for Mac OS X Chapter 8,
You need to set the sortKey to
personName.length
and the selector tocompare:
for the column.Then the sort descriptor will sort by 'comparing' the string 'lengths'.
macOS 开发(阅读 BNR 的书)和 Swift 语言初学者的答案:
Swift 字符串没有
length
属性。您可以通过其字符数组的计数来访问其长度。要实现此目的,您应该将
sortKey
设置为name.characters.count
,并将selector
设置为compare:
。我猜 BNR 建议您应该找到一些东西来表示字符串的长度,而不是有
length
属性或方法。Answer for beginners of macOS development(reading BNR's book) and Swift language:
Swift string doesn't have a
length
property. You can access its length by the count of its character array.To achieve this you should set the
sortKey
toname.characters.count
andselector
tocompare:
.I guess BNR is suggesting that you should find something to represent the length of a string, rather than there is a
length
property or method.