- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
是否可以设置 NSOutlineView NSTableColumn 及其内容的最大宽度?
提前致谢。
最佳答案
八个月后,我希望您继续选择一些足够好的宽度。然而,如果有人需要这种东西,这里有一个方法。它仍然有很大的局限性,所以请密切关注评论。
NSTableView 和 NSOutlineView 背后的整个理念是,它们在需要时才实例化其内容。这有利于效率,但意味着它们会主动阻止每个单元格的读取属性。要根据其内容调整列的大小,您确实必须查看每个单元格并测量其大小。这永远无法很好地扩展。
也就是说,如果您有一个非常小的 NSOutlineView,并且它都是文本,并且该文本来自数据源,那么这应该可以工作。
// sizeOutlineViewToContents
// This looks at the cells in an NSOutlineView and shrinks its rows and columns to fit, with the following limitations:
// 1. The data must come from an NSOutlineViewDataSource. If the NSOutlineView gets its data through bindings, it will throw an exception.
// 2. Text and images will be handled, but other cell types will shrink to the column minimum width.
// 3. Only visible cells will be measured. Any collapsed cells will be ignored.
// 4. This function has to read the contents of every cell and measure the size of their text. This will be slow, and scale badly. Only use on small views, with a few thousand cells at most.
- (void) sizeOutlineViewToContents:(NSOutlineView*) outlineView;
{
if (outlineView)
{
NSInteger rowCount = [outlineView numberOfRows];
CGFloat maxHeight = 0;
// This implementation doesn't work if the OutlineView data arrive via bindings. If you want to make it work, be my guest.
if (![outlineView dataSource])
{
@throw [NSException exceptionWithName:@"DataSourceMissingException" reason:@"autosizeColumnsOfOutlineView only works when the outlineView has a dataSource." userInfo:nil];
}
else
{
for (NSTableColumn *tableColumn in [outlineView tableColumns])
{
// Start with the minimum width already specified for the column.
CGFloat maxWidth = [tableColumn minWidth];
// Allow the space needed for the cell.
if (maxWidth < [[tableColumn headerCell] cellSize].width)
maxWidth = [[tableColumn headerCell] cellSize].width;
for (NSInteger rowIndex = 0; rowIndex<rowCount; rowIndex++)
{
// Find the formatting information used for this cell.
// For most tables, the generic [tableColumn dataCell] would tell us the formatting for all cells, and be faster.
// Given the way we read the header size, it may be tempting to read the cell size, but this will often just return the size of the most recently displayed cell.
NSCell *cell = [tableColumn dataCellForRow:rowIndex];
// Obtain the actual data in this cell from the dataSource.
id rowItem = [outlineView itemAtRow:rowIndex];
id cellObject = [[outlineView dataSource] outlineView:outlineView objectValueForTableColumn:tableColumn byItem:rowItem];
// If the cell has a formatter, assume it knows what to do with the object.
NSFormatter *cellFormatter = [cell formatter];
if (cellFormatter)
cellObject = [cellFormatter stringForObjectValue:cellObject];
// Text is already the difficult one.
if ([cellObject isKindOfClass:[NSString class]])
{
NSDictionary *cellTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
[cell font], NSFontAttributeName,
nil];
NSString *cellText = cellObject;
// Find the space needed to display the cell text.
CGSize size = [cellText sizeWithAttributes:cellTextAttributes];
// If the current column contains the outline view disclosure arrows, allow for the space needed for indentation.
if (tableColumn == [outlineView outlineTableColumn])
size.width += ([outlineView levelForRow:rowIndex]+1)*[outlineView indentationPerLevel];
// Cells have a small amount of additional space between them, defined by the outline view.
size.width+=[outlineView intercellSpacing].width;
size.height+=[outlineView intercellSpacing].height;
// There seems to be one extra pixel needed to display the text at its full width. I'm not sure where this comes from.
size.width+=1;
// Update the maxima found.
if (maxWidth < size.width)
maxWidth = size.width;
if (maxHeight < size.height)
maxHeight = size.height;
}
else if ([cellObject isKindOfClass:[NSImage class]])
{
// Images just need their exact size.
NSImage *cellImage = cellObject;
CGSize size = [cellImage size];
// Update the maxima found.
if (maxWidth < size.width)
maxWidth = size.width;
if (maxHeight < size.height)
maxHeight = size.height;
}
}
// Having found the widest cell, apply it to the column.
[tableColumn setWidth:maxWidth];
}
}
// Having found the highest cell overall, apply it to the table.
[outlineView setRowHeight:maxHeight];
}
}
关于objective-c - NSOutlineView 设置最大列宽及其内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6681750/
根据Apple's documentation , NSOutlineView 要求 View 中的每个项目都是唯一的,但是,如果这实际上不符合业务需求怎么办?特别是,如何显示这样的内容 Item A
我有一个基于 View 的 NSOutlineView,我正在努力使项目可编辑。如果我单击一个项目来编辑它,它将从我单击的插入点开始,而不是选择整个文本。按 Enter/Return 进行编辑会选择整
我有一个带有复选框的 NSOutlineView。我将复选框状态绑定(bind)到带有键 shouldBeCopied 的节点项。在节点项中,我有 getter 和 setter,如下所示: -(BO
获得一个在 Controller 的 awakeFromNib 中配置的 NSOutlineView,带有数据源和自动保存的扩展节点。 一切都像魅力一样工作。 问题是在某个地方,有人也在保存滚动位置的
我阅读了关于 Outlineviews 和 DND 的另一篇文章,但我无法让我的程序运行。这篇文章的底部是我的项目 zip 的链接。它非常基本,只有一个大纲 View 和按钮。我希望它接收放置在其上的
我正在尝试将项目的实际折叠/展开状态存储在 NSOutlineView 中,以便稍后可以恢复。 NSOutlineViewDelegate 上有两种方法可用: outlineViewItemDidEx
我最近在没有帐户的情况下启动了另一个线程,因此我使用帐户在此处重新发布问题,以便我可以编辑该程序的当前链接,以便其他用户可以关注此问题。我还更新了下面的代码。这是我原来的问题: 我阅读了关于 Outl
看起来我的数据模型有一些问题,所以我时不时会收到此错误展开项目时。有谁知道这个断言意味着什么? 2010-10-20 03:04:47.547 test_tree[60332:613] * -NSOu
我正在尝试创建一个简单的文件夹浏览器,使用 NSOutlineView 显示树结构。将 monoMac 和 Xamarin Studio 与 XCode 界面生成器一起用于 UI。 我现在遇到的问题是
我有一个用 C# 编写的 MonoMac/Xamarin.MAc 项目。在其中,我有一个派生自 NSOutlineView 的自定义类,它显示某些项目的上下文菜单。通过这样做,而不是仅仅分配 Menu
我有一个简单的 NSOutlineView,通过 swift 初始化,有 2 列。我制作了以下非常简单的数据源,希望通过这种方式进行测试,但也许我这种格式是不允许的。我假设 TableView 仅根据
我想显示一个简单的 nsoutlineview 来显示层次结构。 当我通过 InterfaceBuilder 完成此操作时,一切都很好,但现在我尝试以编程方式进行操作,却遇到了一些麻烦。 这里是我用来
我正在开发一个应用程序,该应用程序需要一个源列表,例如在 Finder 中找到的源列表。到目前为止,我已经让核心数据与 NSOutlineView 一起使用,但组标题看起来不太像源列表。真实的源列表组
我想获取/设置NSOutlineView的选择索引路径。我该怎么做? 没有明显的方法来获取/设置选择索引路径。 如果您尝试绑定(bind)选择索引路径,并且没有将它们绑定(bind)到树 Contro
我创建了一个基于 View 的 NSOutlineView ,其中包含图像和徽章(内联按钮)。虽然自定义单元格足够宽,但我遇到以下问题: 因此,右侧有一个奇怪的边距覆盖了我的徽章(NSOutlineV
我有一个 NSOutlineView,上面有几个项目。每人有 3 个 child (地点、照片和社交)。他们都有相同的 child 。假设用户选择了地点,有谁知道我如何检测哪个项目是所选项目的父项?
我对此感到很难过。当涉及到基于 NSOutlineView 的 View 的对象持久性时,我遇到了非常奇怪的行为。目前我有一个对象,其中包含一个 object1 数组,该对象包含一个 object2
我正在使用基于 View 的 NSOutlineView 来创建源列表。NSOutlineView 连接到数据源和委托(delegate)对象。 但是,我似乎找不到需要实现的内容才能利用部分标题(例如
我正在尝试实现一个带有渐变背景的 NSOutlineView 子类: - (void)drawBackgroundInClipRect:(NSRect)clipRect { [gradient
我遇到了一个非常奇怪的问题: 我将 NSOutlineView 设置为 NSTableViewSelectionHighlightStyleSourceList。在某些情况下,第二级不缩进并与根节点显
我是一名优秀的程序员,十分优秀!