iOS监听tableView组头切换事件

本文介绍如何在iOS开发中使用UITableView监听组头的切换事件,包括组头将要显示和组头显示结束的方法实现。通过监测滚动方向和偏移量,可以准确判断组头的上下切换,适用于各种需要响应组头变化的场景。

iOS监听tableView组头切换事件

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section 组头将要出现的时候系统会调用;

- (void)tableView:(UITableView *)tableView didEndDisplayingHeaderView:(UIView *)view forSection:(NSInteger)section 组头出现的时候系统会调用;

利用以上两个方法可以判断出组头被顶出和组头又下拉回来事件,还有其他的组头相关动作可以监听需自己去编写。

_currentSection:当前显示的组头

_isUpScroll:是否是上拉滚动

_isFirstLoad:是否第一次加载tableView

_oldY:滚动的偏移量

 

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section{

    if(!_isUpScroll && (_currentSection - section) == 1){

       //最上面组头(不一定是第一个组头,指最近刚被顶出去的组头)又被拉回来

        _currentSection = section;

        NSLog(@"willDisplayHeaderView显示第%ld组",(long)section);

    }

}

 

- (void)tableView:(UITableView *)tableView didEndDisplayingHeaderView:(UIView *)view forSection:(NSInteger)section{

    if(!_isFirstLoad && _isUpScroll){

        _currentSection = section + 1;

        //最上面的组头被顶出去

        NSLog(@"didEndDisplayingHeaderView显示第%ld组",(long)section + 1);

    }

}

 

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{

    if ([scrollView isEqual: self.tableView]) {

        if (self.tableView.contentOffset.y > _oldY) {

            // 上滑

            _isUpScroll = YES;

            NSLog(@"上滑");

        }

        else{

            // 下滑

            _isUpScroll = NO;

            NSLog(@"下滑");

        }

        _isFirstLoad = NO;

    }

}

 

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{

    // 获取开始拖拽时tableview偏移量

    _oldY = self.tableView.contentOffset.y;

}

好文要顶 关注我 收藏该文  

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值