objective-ccocoanstableview

NSTableView scrollRowToVisible with animation


I am trying to implement an action to scroll to the top of a NSTableView, and the bottom of the NSTableView. I am using scrollRowToVisible but I'd love the action to be animated. Is there a way to do this?


Solution

  • While the NSTableView does not have a scroll property you can directly animate, you can instead, with a bit of math animate the scrolling of the NSClipView that the NSTableView lives in.

    Here is how I did this (within a custom subclass of NSTableView) to smoothly animate the row at rowIndex to be scrolled to the center of the view, if possible:

            NSRect rowRect = [self rectOfRow:rowIndex];
            NSRect viewRect = [[self superview] frame];
            NSPoint scrollOrigin = rowRect.origin;
            scrollOrigin.y = scrollOrigin.y + (rowRect.size.height - viewRect.size.height) / 2;
            if (scrollOrigin.y < 0) scrollOrigin.y = 0;
            [[[self superview] animator] setBoundsOrigin:scrollOrigin];