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 co开发者_运维知识库lumn:(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
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];
I wasn't able to find a convenient solution to this, but I implemented a system that works fairly well:
- Keep a mutable array of column widths.
- Implement the
browser:willDisplayCell:atRow:column:
delegate method. - When you add new content to the table, set the value of your column width array at the new column index to 0.
- 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;
- If the calculated cell width is greater than the stored column width, set the stored value to
cellWidth
and call thesetWidth:forColumn:
of your NSBrowser with the new value.
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:
// don't resize column 0
for i in 1 ... self.browser.lastVisibleColumn {
self.browser.setWidth(self.browser.defaultColumnWidth(), ofColumn:i)
}
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.
精彩评论