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

让人想犯罪 __ 提交于 2019-11-28 20:56:28

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.

Regarding your update: for some reasons i have var currentPosition = CGRectGetMaxY([scrollView visibleRect]); always the same value, i've found better to use NSClipView bounds:

NSClipView *clipView = ...;
NSRect newClipBounds = [clipView bounds];
CGFloat currentPosition = newClipBounds.origin.y + newClipBounds.size.height;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!