1

Let's Say i loaded 100 rows in a table in awakeFromNib: , Now i want to call a method when the vertical scroller hits the bottom. Could anyone let me know how to handle the event of NSScroller hitting the bottom and calling a method when this happens.

john fedric
  • 167
  • 1
  • 11

2 Answers2

4
// In awakeFromNib:
self.scrollView = [self.tableView enclosingScrollView];

// Register delegate to the scrollView content View:
// This will send notification whenever scrollView content view visible frame changes.
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(scrollViewDidScroll:) name:NSViewBoundsDidChangeNotification object:self.scrollView.contentView];

// This Method is called when content view frame changed
- (void)scrollViewDidScroll:(NSNotification *)notification {
    NSScrollView *scrollView = self.scrollView;
    // Test if bottom of content view is reached.
    CGFloat currentPosition = CGRectGetMaxY([scrollView visibleRect]);
    CGFloat contentHeight = [self.tableView bounds].size.height - 5;

    if (currentPosition > contentHeight - 2.0) {
        // YOUR ACTION
    }
}
// Remove observer
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
Krzysztof
  • 1,441
  • 11
  • 22
  • Why you are catching an object of NSClipView in NSScrollView *scrollView = [notification object]; ?? – john fedric Aug 27 '14 at 11:14
  • I am calling a method to load more rows as the if condition satisfies(in "YOUR ACTION" part) but as the new rows are loaded(through reloadData: method) existing rows are jumping upwards and it does not seems to me a smooth scroll, could you help me with this.. – john fedric Aug 30 '14 at 13:57
2

You can detect a scrollview at bottom by checking if tableView.enclosingScrollView.verticalScroller?.floatValue == 1 since the floatValue of vertical scroller varies between 0 and 1.

Faizyy
  • 353
  • 2
  • 15