UIViewController.m 파일 안에서
// register observer
void *context = (__bridge void *)self;
[self.tableView addObserver:self
forKeyPath:@"contentOffset"
options:NSKeyValueObservingOptionNew
context:context];
#pragma mark - KVO Methods
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context
{
// Make sure we are observing this value.
if (context != (__bridge void *)self) {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
return;
}
if ((object == self.tableView) &&
([keyPath isEqualToString:@"contentOffset"] == YES)) {
[self scrollViewDidScrollWithOffset:self.tableView.contentOffset.y];
}
}
- (void)scrollViewDidScrollWithOffset:(CGFloat)scrollOffset
{
// do something
}