Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine if a user has scrolled to the end of an NSTableView

I have an NSTableView, and I would like to know when the user has scrolled to the bottom, so I can perform an action. Not quite sure how to go about this?

UPDATE: Here is how I am calculating the bottom of the table:

-(void)tableViewDidScroll:(CPNotification) notification
{
    var scrollView = [notification object];
    var currentPosition = CGRectGetMaxY([scrollView visibleRect]);
    var tableViewHeight = [messagesTableView bounds].size.height - 100;

    //console.log("TableView Height: " + tableViewHeight);
    //console.log("Current Position: " + currentPosition);

    if (currentPosition > tableViewHeight - 100)
    {
       console.log("we're at the bottom!");
    }
}
like image 505
Sheehan Alam Avatar asked Feb 10 '11 16:02

Sheehan Alam


1 Answers

You can add yourself as an observer (in the NSNotificationCenter sense, not the KVO/Bindings sense) of NSViewBoundsDidChangeNotification from the table's -enclosingScrollView's -contentView and react as necessary based on the visible rectangle.

Update

Do this somewhere (maybe -awakeFromNib):

// Configure the scroll view to send frame change notifications
id clipView = [[tableView enclosingScrollView] contentView];
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(myBoundsChangeNotificationHandler:)
                                             name:NSViewBoundsDidChangeNotification
                                           object:clipView];

Put this somewhere useful:

- (void)myBoundsChangeNotificationHandler:(NSNotification *)aNotification
{

if ([aNotification object] == [[tableView enclosingScrollView] contentView])
    [self doSomethingInterestingIfDocumentVisibleRectSatisfiesMe];

}

Essentially you want to examine the scroll view's -documentVisibleRect to see if maybe the bottom couple of pixels are visible. Remember to account for the possibility of views with flipped coordinate systems - "flipped views" - covered in the Views Programming Guide.

like image 50
Joshua Nozzi Avatar answered Oct 18 '22 16:10

Joshua Nozzi