我以编程方式创建了一个NSTableView,其中包含一组6列,其参数在Enum中定义:
public enum SearchResultFields: Int, Codable, CaseIterable {
case symbol = 0
case name = 1
case securityClass = 2
case countryCode = 3
case currencyCode = 4
case ISIN = 5
public var displayString: String {
switch self {
case .symbol: return "Symbol"
case .name : return "Name"
case .securityClass : return "Type"
case .countryCode : return "Country"
case .currencyCode : return "Currency"
case .ISIN: return "ISIN"
}
}
var macOSDefaultWidth: CGFloat {
switch self {
case .symbol: return 100
case .name: return 200
case .securityClass: return 80
case .countryCode: return 100
case .currencyCode: return 80
case .ISIN: return 100
}
}
当显示表格的窗口出现时,我调整窗口的大小,使其宽度与表格中所有列的宽度完全相同(在表格的每一侧加上16pt的间隔符):
let defaultWidth = SearchResultFields.allCases.map({ $0.macOSDefaultWidth }).reduce(0, +) + 16 * 2
print("deafultWidth = \(defaultWidth)")
let oldFframe = self.searchWindowController?.window?.frame ?? CGRect(x: 200, y: 200, width: 600, height: 400)
let newFrame = self.userSettings.searchWindowFrame ?? CGRect(x: oldFframe.minX, y: oldFframe.minY, width: defaultWidth, height: 600)
self.searchWindowController?.window?.setFrame(newFrame, display: true)
表在NScrollView中创建为.documentView,它不允许自动调整大小,然后将每个列添加到表中,迭代Enum:
self.resultsView.columnAutoresizingStyle = .noColumnAutoresizing
for searchField in SearchResultFields.allCases {
let column = NSTableColumn(identifier: NSUserInterfaceItemIdentifier(rawValue: searchField.displayString))
let headerCell = CustomTableHeaderCell(parentTable: self.resultsView, standards: standards, textCell: searchField.displayString)
column.headerCell = headerCell
column.resizingMask = .userResizingMask
column.width = searchField.macOSDefaultWidth
column.minWidth = 30
column.maxWidth = 1000
let descriptor = NSSortDescriptor(key: searchField.displayString, ascending: true)
column.sortDescriptorPrototype = descriptor
print("Adding Column: \(searchField.displayString) - width: \(column.width)")
self.resultsView.addTableColumn(column)
}
程序运行并显示窗口时,所有打印输出均符合预期:以Enum提供的大小添加列,并且窗口大小正好是所有列宽度加上表的每一侧上的16pts间隔符的总和。
Adding Column: Symbol - width: 100.0
Adding Column: Name - width: 200.0
Adding Column: Type - width: 80.0
Adding Column: Country - width: 100.0
Adding Column: Currency - width: 80.0
Adding Column: ISIN - width: 100.0
deafultWidth = 692.0
width: 692.0
SearchVC Bounds: Optional((0.0, 0.0, 692.0, 572.0))
但是窗口只显示前5列,而不是最后一列。它就在那里,但是您需要滚动视图才能看到它,这不是预期的结果。
有人知道我做错了什么吗?
谢谢
1条答案
按热度按时间ocebsuys1#
感谢@willeke,我发现了为什么会发生这样的事情:我没有为NSTableview设置intercellSpacing,但是当我打印出来的时候,它被设置为17(我不知道这个值是从哪里来的)。
将.intercelleSpacing设置为{0,0}解决了我的问题,现在视图显示为所有列都应该显示的样子