检查UIScrollView是否从子单元格滚动

时间:2012-05-21 05:47:01

标签: iphone objective-c ios ipad uiscrollview

所以我有一个UITableViewCell子类,需要知道UITableView当前是否滚动才能更新UI。所以我有一个属性指向子类中的UITableView,我有以下方法代理UIScrollView委托:

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

}

滚动本质上是一个NSNumber,用于指示滚动视图是否滚动。我将此传递给UITableViewCell以便稍后在类中使用,以查看状态是否滚动。我打算使用KVO但是只用一个BOOL就不可能做到这一点(或者如果有可能让我知道的话)。有更优雅的方式吗?

我的UITableViewCell中有一个assign属性,如下所示

@property (nonatomic, assign) BOOL isScrolling

当我启动我的UITableViewCell子类时,我正在协助isScrolling与UITableView上的isScrolling。我想我对这种方法的最大担心是,如果我在UIScrollView委托上更改isScrolling,那么UITableViewCell子类上的isScrolling属性是否也会反映这种变化?

1 个答案:

答案 0 :(得分:0)

不,它不会那样做,您需要手动通知可见单元格有关更改。编写一个方法为您完成工作,并从委托方法中调用它:

- (void)notifyCellsOfScrolling:(BOOL)isScrolling_
{
    // Small optimization, if the state has not changed we don't need to notify.
    if (self.isScrolling == isScrolling_)
        return;

    self.isScrolling = isScrolling_;
    for (UITableViewCell *cell in [self.tableView visibleCells])
        [cell setIsScrolling:isScrolling_];
}

您还必须从cellForRowAtIndexPath设置正确的状态。