NSTableColumn大小以适合内容 [英] NSTableColumn size to fit contents

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

问题描述

我正在和在Mac OS X 10.6(Snow Leopard)上进行开发.当我在两个NSTableView的列标题之间双击时,左侧的列会自动调整大小,就像您期望的那样.

I am developing in and against Mac OS X 10.6 (Snow Leopard). When I double-click between two of my NSTableView's column headers, the column on the left autosizes, like you would expect.

我也想在上下文菜单中提供此功能,但是似乎没有可公开访问的功能来执行此操作.我已经用Google搜索了,看了NSTableView,NSTableHeaderView和NSTableColumn的文档,但是什么也没找到.我发现很难相信当他们显然已经编写了代码来做到这一点时,他们不会公开这么有用的东西.

I want to provide this in a context menu as well, but it seems there is no publicly accessible function to do this. I've Googled, and looked at the documentation for NSTableView, NSTableHeaderView, and NSTableColumn, but found nothing. I find it hard to believe they wouldn't expose something so useful when they obviously have the code written to do it.

我看到了-[NSTableColumn sizeToFit] 方法,但这仅考虑了标头的大小.我也愿意将双击事件发送到NSTableHeaderView,但也无法弄清楚该怎么做.

I saw the -[NSTableColumn sizeToFit] method, but that only takes the header's size into account. I would also settle for sending the double-click event to the NSTableHeaderView, but couldn't figure out how to do that, either.

更新-我意识到提到我有一个NSArrayController(子类)将数据提供给我的表很重要,因此我没有在其上的 NSTableViewDataSource 可以调用-[tableView:objectValueForTableColumn:row:] .这就是问题的症结所在:每一列都绑定到数组控制器的键路径,这就是它获取数据的方式,因此它无法遍历其内容.

Update - I realized it's important to mention I have an NSArrayController (subclass) providing the data to my table, so I don't have an NSTableViewDataSource on which I could call -[tableView: objectValueForTableColumn: row:]. That is the crux of the problem: each column is bound to a keypath of the array controller and that's how it gets its data, so it can't loop through its contents.

推荐答案

您可以通过从列中获取单元格来获取每个单元格的长度.

You can get the length of each cell by getting the cells from the column.

NSCell myCell = [column dataCellForRow:i];

然后获取单元格的宽度.

Then get the width of the cell.

width = [myCell cellSize].width;

之后,您可以将单元格的宽度设置为列的宽度.

After that you can set the width of the cell as the width of the column.

[column setMinWidth:width];
[column setWidth:width];

这篇关于NSTableColumn大小以适合内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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