從OS X 10.10.2開始,基於視圖的表格視圖中的列在雙擊列分隔符時仍不會自動調整大小。
我實現了代表可視矩形的樣本行以及50行以下和以上的代表。
- (CGFloat)tableView:(NSTableView *)tableView sizeToFitWidthOfColumn:(NSInteger)column
{
// Note: Called on double-click of table columns separator
// Note: As of 10.10.2, [NSTableView _sizeToFitWidthOfColumn] does nothing for view-based table views
NSRect visibleRect = tableView.visibleRect;
NSRange rowRange = [tableView rowsInRect:visibleRect];
NSUInteger numberOfRows = tableView.numberOfRows;
NSInteger minRow = rowRange.location;
NSInteger maxRow = rowRange.location + rowRange.length;
minRow = MAX(0, minRow - 50);
maxRow = MIN(numberOfRows, maxRow + 50);
NSTableColumn *tableColumn = [[tableView tableColumns] objectAtIndex:column];
CGFloat minWidth = tableColumn.minWidth;
CGFloat maxWidth = tableColumn.maxWidth;
CGFloat width = minWidth;
/*
NSCell *headerCell = tableColumn.headerCell;
if ([headerCell isKindOfClass:[NSCell class]]) {
NSRect bounds = NSMakeRect(0.0f, 0.0f, maxWidth, headerCell.cellSize.height);
CGFloat fittingWidth = [headerCell cellSizeForBounds:bounds].width;
if (fittingWidth > width) {
width = fittingWidth;
}
}
*/
for (NSInteger row = minRow; row < maxRow; row++) {
NSTableCellView *tableCellView = [tableView viewAtColumn:column row:row makeIfNecessary:YES];
CGFloat fittingWidth = [tableCellView fittingSize].width;
if (fittingWidth > width) {
width = fittingWidth;
if (width >= maxWidth) {
break;
}
}
}
return MIN(maxWidth, width);
}