iOS監聽tableView組頭切換事件

來源:互聯網
上載者:User

標籤:

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

}

iOS監聽tableView組頭切換事件

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.