Search code examples
swiftnstableviewappkitmacos-big-sur

Visual glitch with NSTableView using alternating row colors under macOS Big Sur


I'm using an NSTableView with usesAlternatingRowBackgroundColors set to true.

As soon as I

  1. Add many columns, e.g., 15 and
  2. Set the Cell Spacing height to something > 0

the table shows a visual glitch, where the alternating rows are not equally distributed in height:

enter image description here

This happens only under macOS Big Sur. macOS Mojave and macOS Catalina work fine. I've experimented with almost any combination of settings and styles, using latest Xcode 12.4.

My ViewController is fairly simple:

class ViewController: NSViewController {

    @IBOutlet weak var tableView: NSTableView!

    override func viewDidLoad() {
        super.viewDidLoad()

        for columnIndex in 0..<15 {
            let tableColumn = NSTableColumn(identifier: NSUserInterfaceItemIdentifier(rawValue: "\(columnIndex)"))
            tableColumn.title = "CustomColumn \(columnIndex)"
            tableColumn.width = 150
            tableView.addTableColumn(tableColumn)
        }
    }
}

And the configuration of the NSTableView in Interface Builder is also quite boring, except for the adjusted cell spacing height:

enter image description here

It would be great if someone could confirm the issue and possibly share any workaround for that.

You can find a demo project at https://github.com/fheidenreich/table-test


Solution

  • I've talked to an Apple engineer over this issue during WWDC'21 and he confirmed the issue and proposed an interesting workaround:

    override func drawBackground(inClipRect clipRect: NSRect)
    {
       super.drawBackground(inClipRect: clipRect)
    }
    

    This triggers a different code path with less optimizations inside AppKit and also prevents the issue from appearing.

    I've also updated my feedback to Apple with more examples and it seems that the issue is finally resolved with macOS Monterey 12.0 Beta 5 (21A5304g).