我正在手动计算NSTableColumn的最小大小,以避免文本省略。计算结果如下所示:
NSCell *cell = [m_view preparedCellAtColumn:m_column row:row];
int width = ceil([cell cellSize].width);
if(m_column == [m_view outlineTableColumn]) {
width += [m_view indentationPerLevel] * [m_view levelForRow:row];
width += m_expander;
}m_expander值的计算方法如下:
// assume that row 0 has an expander
NSRect rc = [m_view frameOfOutlineCellAtRow:0];
m_expander = ceil(rc.origin.x + rc.size.width);这在10.13上运行得很好。请参见此屏幕截图:

然而,在macOS 11上,计算不正确,文本被省略:

调试表明,macOS 11上的m_expander缺少4个像素。在macOS 10.13上,rc.origin.x为6,rc.size.width为12。在macOS 11上,rc.origin.x为2,rc.size.width为12。但是,这似乎是正确的,因为您可以看到,在macOS 11上,扩展器左侧的空间比macOS 10.13上的小,因此这些值看起来是正确的。10.13和11之间的所有其他值都是相同的。10.13计算的列宽是169,macOS 11计算的列宽是165。
但是,正如您所看到的,macOS 11上的总列宽计算不正确,因为文本被省略了。有谁知道问题出在哪里了吗?
发布于 2021-06-29 20:01:28
基于单元的NSTabelView已经被弃用,维护得不太好,并且出现了buggy。
有人知道问题出在哪里了吗?
让我们检查一下macOS 10.13的边框和大小:
m_view.tableColumns[0].width: 116
[m_view frameOfCellAtColumn:0 row:0]: (origin = (x = 17, y = 1), size = (width = 100, height = 17))
[m_view frameOfOutlineCellAtRow:0]: (origin = (x = 6, y = 1), size = (width = 12, height = 16))
[m_view preparedCellAtColumn:column row:0].cellSize: (width = 61.662109375, height = 17)
[m_view frameOfCellAtColumn:0 row:1]: (origin = (x = 33, y = 20), size = (width = 84, height = 17))
[m_view frameOfOutlineCellAtRow:1]: (x = 22, y = 20), size = (width = 12, height = 16))
[m_view preparedCellAtColumn:column row:1].cellSize: (width = 66.83544921875, height = 17)
[m_view frameOfCellAtColumn:0 row:2]: (x = 49, y = 39), size = (width = 68, height = 17))
[m_view frameOfOutlineCellAtRow:2]: (x = 0, y = 0), size = (width = 0, height = 0))
[m_view preparedCellAtColumn:column row:2].cellSize: (width = 108.9267578125, height = 17)
[m_view indentationPerLevel]: 16计算:
NSRect rc = [m_view frameOfOutlineCellAtRow:0];
m_expander = ceil(rc.origin.x + rc.size.width);
m_expander: 18
NSCell *cell = [m_view preparedCellAtColumn:m_column row:row];
int width = ceil([cell cellSize].width);
width += [m_view indentationPerLevel] * [m_view levelForRow:row];
width += m_expander;
row 0, width = 80
row 1, width = 101
row 2, width = 159但是文本没有被截断的最小列宽是157。第0行的单元格从x= 17开始,而不是从18开始。显然,单元格之间的间距不是0。
解决方案:
将单元格的当前大小与单元格的最小大小之间的差值与列的当前大小相加。
CGFloat widthToAdd = -CGFLOAT_MAX;
for (NSUInteger row = 0; row < m_view.numberOfRows; row++) {
CGFloat requiredCellWidth = [m_view preparedCellAtColumn:m_column row:row].cellSize.width;
CGFloat currentCellWidth = [m_view frameOfCellAtColumn:m_column row:row].size.width;
CGFloat delta = requiredCellWidth - currentCellWidth;
if (delta > widthToAdd)
widthToAdd = delta;
}
NSTableColumn *column = m_view.tableColumns[m_column];
CGFloat newMinWidth = column.width + ceil(widthToAdd);
if (column.width < newMinWidth)
column.width = newMinWidth; // set width before minWidth, setting minWidth first doesn't update the column
column.minWidth = newMinWidth;https://stackoverflow.com/questions/68057117
复制相似问题