10

我通过发送将 NSTableView 中的单元间距设置为 0:

[self.tableView setIntercellSpacing:NSMakeSize(0, 0)];

在窗口控制器中,awakeFromNib但行之间仍然有一个(可能是 1 像素宽)空白空间,我认为这是绘制网格线的地方,尽管我没有使用网格线。我怎样才能摆脱行之间的这个空间?

更新:

NSTableView 文档似乎说,当单元间分隔设置为 0、0 时,这个 1 像素分隔应该消失。在我的情况下,它不是。也许这是一个错误?

4

2 回答 2

5

正如 trudyscousin 所建议的,我将发布我如何解决我的问题:

事实证明,当您像我一样将单元间距设置为 0 时,空白空间实际上会消失。我的问题是我的NSTableCellView子类中的绘图代码没有一直绘制到视图的边缘。我看到的差距不是小区间的分离,而是我的NSTableCellView子类的边界。

于 2012-12-09T08:33:13.720 回答
1

就我而言,任务是使用0.5 pt(Retina 显示屏上 1 px)分隔符。似乎即使intercellSpacing设置为.zero(或在我的情况下为 0.5 pt)AppKit 在绘制选择时仍保留行之间的 1 pt 空间。

我最终通过子类化NSTableRowView。使用自定义行,我可以将分隔符设置为任何高度。这是一个 Swift 4.2 示例:

class WelcomeRecentDocumentsView: View {

   private lazy var tableView = TableView().autolayoutView()
   private lazy var scrollView = ScrollView(tableView: tableView).autolayoutView()

   override var intrinsicContentSize: NSSize {
      return CGSize(width: 240, height: 400)
   }

   private var recentDocumentsURLs = (0 ..< 10).map { $0 }
}

extension WelcomeRecentDocumentsView: NSTableViewDataSource {

   func numberOfRows(in tableView: NSTableView) -> Int {
      return recentDocumentsURLs.count
   }

   func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
      return tableView.makeView(ItemView.self)
   }

}

extension WelcomeRecentDocumentsView: NSTableViewDelegate {

   func tableView(_ tableView: NSTableView, rowViewForRow row: Int) -> NSTableRowView? {
      // Returning custom RowView.
      return RowView(separatorHeight: separatorHeight, separatorColor: tableView.gridColor)
   }
}

extension WelcomeRecentDocumentsView {

   private var separatorHeight: CGFloat {
      return convertFromBacking(1)
   }

   override func setupUI() {
      addSubview(scrollView)

      let column = NSTableColumn(identifier: "com.example.column", resizingMask: .autoresizingMask)
      tableView.addTableColumn(column)
      tableView.columnAutoresizingStyle = .uniformColumnAutoresizingStyle
      tableView.headerView = nil
      tableView.setAutomaticRowHeight(estimatedHeight: ItemView.defaultHeight)
      tableView.intercellSpacing = CGSize(height: separatorHeight)
      tableView.gridStyleMask = .solidHorizontalGridLineMask
   }

   override func setupHandlers() {
      tableView.delegate = self
      tableView.dataSource = self
   }

   override func setupLayout() {
      LayoutConstraint.pin(to: .bounds, scrollView).activate()
   }

   override func setupDefaults() {
      tableView.sizeLastColumnToFit()
   }
}

extension WelcomeRecentDocumentsView {

   class RowView: NSTableRowView {

      let separatorHeight: CGFloat
      let separatorColor: NSColor

      init(separatorHeight: CGFloat = 1, separatorColor: NSColor = .gridColor) {
         self.separatorHeight = separatorHeight
         self.separatorColor = separatorColor
         super.init(frame: .zero)
      }

      required init?(coder decoder: NSCoder) {
         fatalError()
      }

      override func drawSelection(in dirtyRect: NSRect) {
         let rect = bounds.insetBottom(by: -separatorHeight)
         NSColor.alternateSelectedControlColor.setFill()
         rect.fill()
      }

      override func drawSeparator(in dirtyRect: NSRect) {
         let rect = bounds.insetTop(by: bounds.height - separatorHeight)
         separatorColor.setFill()
         rect.fill()
      }

   }

   class ItemView: View {

      static let defaultHeight: CGFloat = 52

      override var intrinsicContentSize: NSSize {
         return CGSize(intrinsicHeight: type(of: self).defaultHeight)
      }
   }
}
于 2018-09-21T04:35:25.137 回答