调整 NSBrowser 列的大小以适合内容的宽度
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个:
或者,如果您想调整整个浏览器的大小,您可以尝试:
Try this:
Or, if you want to resize the entire browser, you could try:
我无法找到一个方便的解决方案,但我实现了一个运行得相当好的系统:
willDisplayCell
委托时,计算每个单元格的宽度。对于 NSTextFieldCells,我使用: CGFloat cellWidth = [((NSTextFieldCell *)cell).attributedStringValue size].width + 40.0f;cellWidth
并使用新值调用 NSBrowser 的setWidth:forColumn:
。I wasn't able to find a convenient solution to this, but I implemented a system that works fairly well:
browser:willDisplayCell:atRow:column:
delegate method.willDisplayCell
delegate is called, calculate the width for each cell. For NSTextFieldCells, I used:CGFloat cellWidth = [((NSTextFieldCell *)cell).attributedStringValue size].width + 40.0f;
cellWidth
and call thesetWidth:forColumn:
of your NSBrowser with the new value.Swift 3 更新:
尽管这是一个老问题,但这个问题仍然相关,并且只得到了部分答案。乔什上面的答案非常好,但我认为没有必要维护额外的列宽数组。我解决这个问题的方法是:
向浏览器添加一个操作处理程序:
browser.action = #selector(BrowserController.selectionChanged)
在selectionChanged中,将列宽重置为默认值:
正如 Josh 提到的,实现 browser:willDisplayCell:atRow:column: 方法。在该方法中,将列的大小调整为其中最大的行,并添加一点填充:
执行这两项操作将首先重置列,然后将其调整为最大行的大小。
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:
add an action handler to the browser:
browser.action = #selector(BrowserController.selectionChanged)
in selectionChanged, reset the column widths to the default:
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:
Doing both these things will first reset the column, then size it to the size of the biggest row.