I've got a view-based NSTableView showing a timeline of messages/informations. Row heights are variable. New messages are regularly added at the top of the table by using insertRows
:
NSAnimationContext.runAnimationGroup({ (context) in
context.allowsImplicitAnimation = true
self.myTable.insertRows(at: indexSet, withAnimation: [.effectGap])
})
While the user stays at the top of the table, the messages keep being inserted at the top, pushing down the existing ones: a usual behavior in this context.
Everything works ok, except that if the user has scrolled down, the new inserted messages shouldn't make the table scroll.
I want the tableView to stay where it is while the user scrolls or if the user has scrolled down.
In other words, the tableView should only be pushed down by new inserted rows if the top row is 100% visible.
I've tried to give the illusion of the table not moving by quickly restoring its location like this:
// we're not at the top anymore, user has scrolled down, let's remember where
let scrollOrigin = self.myTable.enclosingScrollView!.contentView.bounds.origin
// stuff happens, new messages have been inserted, let's scroll back where we were
self.myTable.enclosingScrollView!.contentView.scroll(to: scrollOrigin)
But it doesn't behave as I want. I've tried many combinations but I think I'm not understanding something about the relation between the clip view, the scroll view and the table view.
Or maybe I'm in the XY-problem zone and there's a different way to get this behavior?