NSTableView基于NSStrings的行高 [英] NSTableView Row Height based on NSStrings

查看:223
本文介绍了NSTableView基于NSStrings的行高的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我有一个NSTableView与1 collumn,我插入长字符串到每一行。但是,并非所有的字符串都很长,所以我希望每一行的高度根据字符串的长度而不同。

Basically, I have an NSTableView with 1 collumn, and I'm inserting long strings into each row. However, not all the strings are long, so I'd like the height of each row to be different based on how long the string is.

我想出了我需要询问collumn它有多宽,然后问字符串如果collumn是宽,将占用多少行,然后决定如何高NSCell将是。但是我该怎么做呢?我已经得到了collumn宽度:

I've figured out that I need to ask the collumn how wide it is, and then ask the string how many lines it will take up if the collumn is that wide, and then decide how "tall" the NSCell will be. But how exactly do I go about doing that? I've gotten the collumn width from:

[[[tableView tableColumns] objectAtIndex:0] width];

但我不知道如何询问NSString它将占用多少空间。

but I can't figure out how to ask the NSString how much space it will take up. Or, perhaps, there is a better way that I should go about doing this?

感谢您提供任何协助。

推荐答案

创建 NSTextFieldCell 实例并匹配其字体/大小/等。到您的列的数据单元格。请求它为 -cellSizeForBounds:传递所需宽度的矩形,具有大的高度(FLT_MAX?)。结果应该是一个NSSize,它的高度可以使用。

Create an NSTextFieldCell instance and match its font/size/etc. to that of your column's data cell. Ask it for the -cellSizeForBounds:, passing in a rect of the desired width for the column, with a large height (FLT_MAX?). The result should be an NSSize whose height you can use.

如果你有多个多行文本列,它会变得棘手,因为你需要考虑所有该行中的单元格,将最大的作为您的行高。如果你平均需要很多行,你可能需要缓存这个工作,根据需要更新它,然后在调用行高代理方法时引用它。

It gets trickier if you have more than one multi-line text column because you'll need to consider all the cells in that row, taking the largest as your row height. If you expect a lot of rows on average, you'll probably want to cache this work, updating it as necessary, then simply referencing it when the row height delegate method is called.

这篇关于NSTableView基于NSStrings的行高的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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