iOS 监听section 组头变化

#define headerHeight 40

@interface TestTableViewController ()
{
    //是否上拉
    BOOL _isScrollUp;
    NSMutableDictionary<NSString*,UIView*> *headerViews;
}
@end

@implementation TestTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    headerViews = [NSMutableDictionary new];
    self.tableView.rowHeight = 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return headerHeight;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.000000001;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{

    NSString *key = [NSString stringWithFormat:@"%zd",section];
    UIView *header = headerViews[key];
    
    if (!header) {
        header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 375, headerHeight)];
        header.tag = section;
        header.backgroundColor = [UIColor lightGrayColor];
        
        UILabel *label = [[UILabel alloc] init];
        label.text = [NSString stringWithFormat:@"第%zd组",section];
        [label sizeToFit];
        
        label.center = header.center;
        
        [header addSubview:label];
        
        //缓存组头
         headerViews[key] = header;
        
        //观察坐标变化
        [header addObserver:self forKeyPath:@"frame" options:(NSKeyValueObservingOptionNew) context:nil];
    }
    return header;
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 20;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 5;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    if(!cell){
        cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"cell"];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"第%zd行",indexPath.row];
    return cell;
}


- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
    
    UIView *header = (UIView*)object;
    
    
    //转化成相对屏幕的x坐标
    CGRect rect = [self.tableView convertRect:header.frame toView:self.tableView.superview];
    
    NSInteger tag = header.tag;

    //上拉
    if (_isScrollUp) {
        
        CGFloat naviBarHeight = [self getNavigaHeight];
        
        //最上面的headerview完全消失
        if (rect.origin.y < naviBarHeight - headerHeight) {
            
            [self currentHeader:tag+1];
        }
    }else{

        CGFloat limitY = [self isFringe]?88:64;
        //刘海屏为什么是88?不是84
        
        ///y坐标 = 导航条高, 刚好位于导航条下面,header完全出现
        if (rect.origin.y == limitY ) {
            [self currentHeader:tag];
        }
    }
}

///当前header
- (void)currentHeader:(NSInteger)tag{
    NSString *key = [NSString stringWithFormat:@"%zd",tag];
    UIView *view = headerViews[key];
    
    [self reset];
    if (view) {
        [self shadow:view];
    }
}

- (void)shadow:(UIView*)view{
    
    view.layer.shadowColor = [UIColor redColor].CGColor;
    // 阴影偏移量 默认为(0,3)
    view.layer.shadowOffset = CGSizeMake(4, 4);
    // 阴影透明度
    view.layer.shadowOpacity = 1;
}

//恢复默认样式
- (void)reset{
    NSArray *views = headerViews.allValues;
    [views enumerateObjectsUsingBlock:^(UIView * _Nonnull view, NSUInteger idx, BOOL * _Nonnull stop) {
        // 阴影偏移量 默认为(0,3)
        view.layer.shadowOffset = CGSizeZero;
        // 阴影透明度
        view.layer.shadowOpacity = 0;
    }];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    static CGFloat lastOffsetY = 0;
    _isScrollUp = lastOffsetY < scrollView.contentOffset.y;
    lastOffsetY = scrollView.contentOffset.y;
}

//导航条高
- (CGFloat)getNavigaHeight{
    if ([self isFringe]) {
        return 84;
    }
    return 64;
}

//刘海屏
- (BOOL)isFringe{
    BOOL iPhoneXSeries = NO;
    if (UIDevice.currentDevice.userInterfaceIdiom != UIUserInterfaceIdiomPhone) {
        return iPhoneXSeries;
    }
    
    if (@available(iOS 11.0, *)) {
        UIWindow *mainWindow = [[[UIApplication sharedApplication] delegate] window];
        if (mainWindow.safeAreaInsets.bottom > 0.0) {
            iPhoneXSeries = YES;
        }
    }
    return iPhoneXSeries;
}
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 226,130评论 6 524
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 97,210评论 3 410
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 173,639评论 0 370
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 61,755评论 1 304
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 70,694评论 6 404
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 54,160评论 1 317
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 42,359评论 3 433
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 41,436评论 0 282
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 47,990评论 1 328
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 39,981评论 3 351
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 42,089评论 1 359
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 37,669评论 5 353
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 43,363评论 3 342
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 33,762评论 0 25
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 34,950评论 1 278
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 50,689评论 3 384
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 47,124评论 2 368

推荐阅读更多精彩内容