Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to add observer to tableView.contentOffset?

I need to track tableView.contentOffset.y Is it possible to add observer to tableView.contentOffset?

I think this is impossible because contentOffset doesn't inherit NSObject class.

Is any other solution?

like image 583
Voloda2 Avatar asked Aug 23 '12 08:08

Voloda2


2 Answers

UITableView is a UIScrollView subclass so you can use the UIScrollViewDelegate method scrollViewDidScroll: to be notified when the view scrolled. Check the contentOffset of the scrollView in that method

contentOffset is a key path, so you can also observe its changes using KVO

[self.tableView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
like image 152
wattson12 Avatar answered Sep 21 '22 09:09

wattson12


Swift 5

tableContentObserver = table.observe(\UITableView.contentOffset, options: .new) { [weak self] table, change in
    self?.navigationItem.rightBarButtonItem?.title = "\(change.newValue)"
}
like image 23
Mike Glukhov Avatar answered Sep 21 '22 09:09

Mike Glukhov