objective-c - NSOutlineView 设置最大列宽及其内容

标签 objective-c cocoa macos nstableview nsoutlineview

是否可以设置 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/

相关文章:

python - 为 python 设置 virtualenv 时出现错误的解释器错误

ios - 在 xcode 中跟踪僵尸对象

objective-c - 如何在完成其他 NSThread 后启动 NSThread?

ios - 将数据从子模态 VC 传递到父 View Controller 的最佳方式?

objective-c - 在 Cocoa 应用程序中使用什么更好 : dynamic libraries or static libraries?

cocoa - NSTableView 正确处理编辑单元格

objective-c - 如何在我的 View Controller 中更新 UITextView?

cocoa - 如何避免该函数中的内存泄漏?

objective-c - NSDatePicker 时区怪异

macos - Xcode 12 : how to link against object files built for free standing