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