1

假设我在 awakeFromNib: 的表中加载了 100 行,现在我想在垂直滚动条到达底部时调用一个方法。谁能让我知道如何处理 NSScroller 触底事件并在发生这种情况时调用方法。

4

2 回答 2

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];
}
于 2014-08-27T10:38:06.620 回答
2

您可以通过检查 tableView.enclosureScrollView.verticalScroller?.floatValue == 1 来检测底部的滚动视图,因为垂直滚动条的 floatValue 在 0 和 1 之间变化。

于 2018-11-30T12:56:45.283 回答