调整 NSBrowser 列的大小以适合内容的宽度

发布于 2024-10-08 08:32:22 字数 1093 浏览 0 评论 0原文

我正在使用 NSBrowser(10.6SDK/Cocoa 应用程序),我想向浏览器列发送一条消息,使其调整其宽度以匹配该列中最大单元格的宽度。

在概念上似乎非常基本,尽管应该有一种方法,但没有。

这里有人之前解决过这个问题吗?你是怎么解决的?代码示例?

// 更新 我添加了 drop 支持。该代码有效,但列中的新项目不适合(有椭圆)。

-(BOOL) browser:(NSBrowser *)browser acceptDrop:(id <NSDraggingInfo>)info atRow:(NSInteger)row column:(NSInteger)column dropOperation:(NSBrowserDropOperation)dropOperation
 {
NSPasteboard* pboard = [info draggingPasteboard];
NSData* rowData = [pboard dataForType:CMCourseDataType];
NSIndexSet* rowIndexes = [NSKeyedUnarchiver unarchiveObjectWithData:rowData];

NSArray *selectedCourses = [[self coursesArrayController] selectedObjects];
Group *selectedGroup = [[self allGroupsArray] objectAtIndex:row];

[selectedGroup addCourses:[NSSet setWithArray:selectedCourses]];
[browser reloadColumn:1];

/* 下面尝试了新代码,导致第 1 列调整其宽度,但它们没有影响 */

[browser sizeToFit];
[[browser matrixInColumn:1] sizeToCells];   

在 awakeFromNib 中,我设置:

[browser setColumnResizingType:NSBrowserUserColumnResizing];

谢谢

I'm using an NSBrowser (10.6SDK/Cocoa app) and I want to send a message to a browser column that will make it resize its width to match the width of the largest cell within that column.

Seems pretty basic in concept, even as though there should be a method for it, but no.

Anyone here tackle this before? How did you solve it? Code sample?

// Update
I've added drop-support. The code works, but the new item in the column doesn't fit (has an ellipse).

-(BOOL) browser:(NSBrowser *)browser acceptDrop:(id <NSDraggingInfo>)info atRow:(NSInteger)row column:(NSInteger)column dropOperation:(NSBrowserDropOperation)dropOperation
 {
NSPasteboard* pboard = [info draggingPasteboard];
NSData* rowData = [pboard dataForType:CMCourseDataType];
NSIndexSet* rowIndexes = [NSKeyedUnarchiver unarchiveObjectWithData:rowData];

NSArray *selectedCourses = [[self coursesArrayController] selectedObjects];
Group *selectedGroup = [[self allGroupsArray] objectAtIndex:row];

[selectedGroup addCourses:[NSSet setWithArray:selectedCourses]];
[browser reloadColumn:1];

/* new code tried below, to cause column 1 to resize its width but they have no affect */

[browser sizeToFit];
[[browser matrixInColumn:1] sizeToCells];   

In awakeFromNib, I set:

[browser setColumnResizingType:NSBrowserUserColumnResizing];

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

窝囊感情。 2024-10-15 08:32:22

试试这个:

NSBrowser *browser; //Browser containing columns
NSInteger index; //Index of column to resize
[[browser matrixInColumn:index] sizeToCells];

或者,如果您想调整整个浏览器的大小,您可以尝试:

[browser sizeToFit];

Try this:

NSBrowser *browser; //Browser containing columns
NSInteger index; //Index of column to resize
[[browser matrixInColumn:index] sizeToCells];

Or, if you want to resize the entire browser, you could try:

[browser sizeToFit];
梦一生花开无言 2024-10-15 08:32:22

我无法找到一个方便的解决方案,但我实现了一个运行得相当好的系统:

  1. 保留一个可变的列宽数组。
  2. 实现 browser:willDisplayCell:atRow:column: 委托方法。
  3. 当您向表格添加新内容时,请将新列索引处的列宽数组的值设置为 0。
  4. 调用 willDisplayCell 委托时,计算每个单元格的宽度。对于 NSTextFieldCells,我使用: CGFloat cellWidth = [((NSTextFieldCell *)cell).attributedStringValue size].width + 40.0f;
  5. 如果计算的单元格宽度大于存储的列宽,则设置存储的列宽值到 cellWidth 并使用新值调用 NSBrowser 的 setWidth:forColumn:

I wasn't able to find a convenient solution to this, but I implemented a system that works fairly well:

  1. Keep a mutable array of column widths.
  2. Implement the browser:willDisplayCell:atRow:column: delegate method.
  3. When you add new content to the table, set the value of your column width array at the new column index to 0.
  4. As the willDisplayCell delegate is called, calculate the width for each cell. For NSTextFieldCells, I used: CGFloat cellWidth = [((NSTextFieldCell *)cell).attributedStringValue size].width + 40.0f;
  5. If the calculated cell width is greater than the stored column width, set the stored value to cellWidth and call the setWidth:forColumn: of your NSBrowser with the new value.
驱逐舰岛风号 2024-10-15 08:32:22

Swift 3 更新:

尽管这是一个老问题,但这个问题仍然相关,并且只得到了部分答案。乔什上面的答案非常好,但我认为没有必要维护额外的列宽数组。我解决这个问题的方法是:

  1. 向浏览器添加一个操作处理程序:

    browser.action = #selector(BrowserController.selectionChanged)

在selectionChanged中,将列宽重置为默认值:

    // don't resize column 0
    for i in 1 ... self.browser.lastVisibleColumn {
        self.browser.setWidth(self.browser.defaultColumnWidth(), ofColumn:i)
    }
  1. 正如 Josh 提到的,实现 browser:willDisplayCell:atRow:column: 方法。在该方法中,将列的大小调整为其中最大的行,并添加一点填充:

     let colWidth = browser.frame(ofColumn: column).size.width //当前宽度
    
        如果 cell.cellSize.width + 15 > colWidth { //cell 是委托方法中的当前单元格 
            browser.setWidth(fscell.cellSize.width + 15, ofColumn: 列)
        }
    

执行这两项操作将首先重置列,然后将其调整为最大行的大小。

Update for Swift 3:

Even though this is an old question, this question is still relevant and only partially answered. Josh's answer above is pretty good, but I don't think it is necessary to maintain an extra array of column widths. What I did to solve this is:

  1. add an action handler to the browser:

    browser.action = #selector(BrowserController.selectionChanged)

in selectionChanged, reset the column widths to the default:

    // don't resize column 0
    for i in 1 ... self.browser.lastVisibleColumn {
        self.browser.setWidth(self.browser.defaultColumnWidth(), ofColumn:i)
    }
  1. as Josh mentions, implement the browser:willDisplayCell:atRow:column: method. In that method, size the column to the biggest row in it with a little padding:

        let colWidth = browser.frame(ofColumn: column).size.width //current width
    
        if cell.cellSize.width + 15 > colWidth { //cell is the current cell in the delegate method 
            browser.setWidth(fscell.cellSize.width + 15, ofColumn: column)
        }
    

Doing both these things will first reset the column, then size it to the size of the biggest row.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文