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;

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM