Search code examples
iosuitableviewobserver-pattern

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?


Solution

  • 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];