-1

I have a view-based NSTableView, there are multiple rows (e.g, 20), everytimes when i scroll down the scroller, i want it to scroll to the next row.

For example, right now row 5 is selected, then scroll down will select row 6. But right now, the scroll view can stop in the center between row 5 and row 6.

So my question is that is there any way to set "scroll step", when cell height is 50, then scroll up/down 50 every time.

NotificationCenter.default.addObserver(self,
                             selector: #selector(scollDidEnd(_:)),
                             name: NSScrollView.didLiveScrollNotification,
                             object: tableView.enclosingScrollView)

@objc func scollDidEnd(_ notification : Notification){


}
jimwan
  • 1,086
  • 2
  • 24
  • 39
  • Scroll down will not change the selection. How do you scroll one step? – Willeke Oct 12 '18 at 16:16
  • @Willeke, i want when the scroll is stopped, the top visible of the tableview is just the top of one cell, rather than the center between two cells – jimwan Oct 13 '18 at 03:12
  • Did you try `visibleRect`, `rows(in:)` and `scrollRowToVisible(_:)`? – Willeke Oct 13 '18 at 11:31

1 Answers1

0

Here's my objective-c solution. The target row scrolls to 2/3 of the height of the visibleRect, which is a compromise for my particular app. Adjust the 2/3 math to fit your needs.

#pragma mark -
#pragma mark NSTableView category
@implementation NSTableView (VNSTableViewCategory)

-(NSInteger)visibleRow
{
   NSRect theRect = [self visibleRect];
   NSRange theRange = [self rowsInRect:theRect];
   if ( theRange.length == 0 )
      return -1;
   else if ( NSLocationInRange( [self editedRow], theRange ) )
      return [self editedRow];        
   else if ( NSLocationInRange( [self clickedRow], theRange ) )
      return [self clickedRow];      
   else if ( NSLocationInRange( [self selectedRow], theRange ) )
      return [self selectedRow];
   else
      return theRange.location + (theRange.length/2);
}

-(NSRange)visibleRows
{
   NSRect theRect = [self visibleRect];
   NSRange theRange = [self rowsInRect:theRect];
   return theRange;
}

-(void)scrollRowToVisibleTwoThirds:(NSInteger)row
{
   NSRect theVisRect = [self visibleRect];
   NSUInteger numRows = [self numberOfRows];   
   NSRange visibleRows = [self rowsInRect:theVisRect];
   //NSUInteger lastVisibleRow = NSMaxRange(visibleRows);
   if ( NSLocationInRange( row, visibleRows ) )
   {
      if ( row - visibleRows.location < (visibleRows.length * 2 / 3) )
      {
         // may need to scroll up
         if ( visibleRows.location == 0 )
         {
            // top row at least partially visible.  Only scroll if near top
            if ( row == 0 || row == 1 )
                [self scrollRowToVisible:0];
            return;
         }
         else if ((row - visibleRows.location) > 2 )
            return;
      }
   }

   NSRect theRowRect = [self rectOfRow:row];

   NSPoint thePoint  = theRowRect.origin;
   thePoint.y -= theVisRect.size.height / 4; // scroll to 25% from top

   if (thePoint.y < 0 )
      thePoint.y = 0;

   NSRect theLastRowRect = [self rectOfRow:numRows-1];
   if ( thePoint.y + theVisRect.size.height > NSMaxY(theLastRowRect) )
      [self scrollRowToVisible:numRows-1];
   else
   {
      [self scrollPoint:thePoint]; // seems to be the 'preferred' method of doing this

      // kpk note: these other approaches cause redraw artifacts in many situations:
//      NSClipView *clipView = [[self enclosingScrollView] contentView];
//      [clipView scrollToPoint:[clipView constrainScrollPoint:thePoint]];
//      [[self enclosingScrollView] reflectScrolledClipView:clipView];
//      [self scrollRowToVisible:row];

//      [[[self enclosingScrollView] contentView] scrollToPoint:thePoint];
//      [[self enclosingScrollView] reflectScrolledClipView:[[self enclosingScrollView] contentView]];       
   }

}
@end
Keith Knauber
  • 752
  • 6
  • 13