[ACCEPTED]-Is it possible to add observer to tableView.contentOffset?-observer-pattern
Accepted answer
UITableView
is a UIScrollView
subclass so you can use the UIScrollViewDelegate
method 3 scrollViewDidScroll:
to be notified when the view scrolled. Check 2 the contentOffset
of the scrollView
in that method
contentOffset
is a key path, so 1 you can also observe its changes using KVO
[self.tableView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
Swift 5
tableContentObserver = table.observe(\UITableView.contentOffset, options: .new) { [weak self] table, change in
self?.navigationItem.rightBarButtonItem?.title = "\(change.newValue)"
}
0
Swift 3
Add an observer for the contentOffset
key path using 2 Key-Value Observing (KVO):
tableView.addObserver(self, forKeyPath: #keyPath(UIScrollView.contentOffset), options: [.old, .new], context: nil)
And handle notifications 1 for changes:
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if keyPath == #keyPath(UIScrollView.contentOffset) {
// Your code
}
}
Source:
stackoverflow.com
More Related questions
Cookie Warning
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.