So I have a UITableViewCell subclass that needs to know whether the UITableView is currently scrolling or not to update the UI. So I had a property pointing to the UITableView in the subclass and I have the following method delegate for the UIScrollView delegate:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGFloat scrollOffset = scrollView.contentOffset.y;
CGFloat contentHeight = scrollView.contentSize.height - kLoadMoreOffset;
if (scrollOffset >= contentHeight && loadMore && [self.nextPaginationURL_ isNotNull]){
loadMore = NO;
[self loadMore];
}
self.isScrolling = [NSNumber numberWithInt:1];
}
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
if (!decelerate){
self.isScrolling = [NSNumber numberWithInt:0];
NSLog(@"FIRING NOTIF END DRAGGING");
[self showLoadMore];
}
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
NSLog(@"FIRING NOTIF END DECEL");
self.isScrolling = [NSNumber numberWithInt:0];
[self showLoadMore];
}
Is scrolling is essentially a NSNumber to indicate whether the scroll view is scrolling or not. I am passing this to the UITableViewCell to later on be used in the class, to see if the state is scrolling or not. I was planning to use KVO but it is impossible to do that with just a BOOL (or if it’s possible let me know). Is there a more elegant way to do this?
I have an assign property in my UITableViewCell as follows
@property (nonatomic, assign) BOOL isScrolling
and when I am initing my UITableViewCell subclass I am assinging isScrolling with the isScrolling on the UITableView. I guess my biggest concern with this approach is that if I change isScrolling on the UIScrollView delegate, will the isScrolling property on the UITableViewCell subclass also reflect the change?
No it will not do that, you need to notify the visible cells manually about the change. Write a method to do the work for you and just call it from the delegate methods:
You must also setup the proper state from
cellForRowAtIndexPath.