I ran into the same issue on a cell-based NSTableView with Swift 5 / MacOS 14.
The NSScrollView enclosing an NSTableView owns both the contentView and the headerView of the NSTableView (and the cornerView, which I do not use), and is normally responsible of coordinating their scrolling.
- When scrolling with mouse, the NSScrollView internal magic handle correctly the scrolling of the header view.
- When scrolling programmatically the NSClipView using scroll(to:) + reflectScrolledClipView, NSScrollView fails to scroll of the headerView.
I use this protocol in order to scroll programmatically the headerView too, which allows me to scroll programmatically using this protocol:
extension NSTableView : ScrollingProtocol {
func getScrollView() -> NSScrollView? {
return enclosingScrollView
}
func getVisibleOrigin() -> NSPoint? {
return enclosingScrollView?.documentVisibleRect.origin
}
func scrollToOrigin(_ targetOrigin: NSPoint) {
guard let currentOrigin = getVisibleOrigin(),
let scrollView = enclosingScrollView
else { return }
if (!NSEqualPoints(targetOrigin, currentOrigin)) {
let clipView = scrollView.contentView
clipView.scroll(to: targetOrigin)
// Workaround because NSClipView.scroll(to:) does not scroll
// the headerView of NSTableView
if let headerView = headerView {
let x = targetOrigin.x
let y = headerView.bounds.origin.y
if let headerClipView = headerView.superview as? NSClipView {
headerClipView.scroll(to: NSMakePoint(x, y))
}
}
scrollView.reflectScrolledClipView(clipView)
}
}
}
-documentViewinstead of-contentView? - user557219-documentViewinstead of-contentViewdoesn't make a whole lot of sense anyway;-documentViewreturns theNSTableViewwhereas-contentViewreturns theNSClipView. In this case, I'm pretty sure I need theNSClipView. - Nate Thorn