Question

In my app I have a NSTableView in NSViewController with a NSArrayController provide the content.

It works great, however, when I scroll to some position of tableview, quit app and re-launch it, It will restore the last scroll position. I don't like this behavior, I want it to keep at the top.

I tried to setAutosaveTableColumns to NO on NSTableView, it seems not the option I need. It still works the old way.

Does is any option to turn this off?

Was it helpful?

Solution

The scroll position is stored by NSScrollView, which implements encodeRestorableStateWithCoder:/restoreStateWithCoder: since OS X Lion.
(Interface Builder automatically wraps a table view in a NSScrollViewNSClipViewNSTableView hierarchy).

To get a scroll view that is scrolled to top after an app relaunch you have several options:

  • Turn off view state restoration for the entire window in Interface Builder: enter image description here
  • Subclass NSScrollView and override encodeRestorableStateWithCoder: with an empty implementation (Don't forget to set it as class for your your table view instance in IB):
  • Programmatically scroll to top after app relaunch:

    - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
    {
        [[self.scrollView contentView] scrollToPoint:NSMakePoint(0.0, 0.0)];
        [self.scrollView reflectScrolledClipView:[self.scrollView contentView]];
    }
    

Personally I'd go with method 3, because turning off state restoration for the whole window might break some behaviour that your users expect and method 2 is against Apple's recommendation on calling [super encodeRestorableStateWithCoder:] when subclassing NSView.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top