UITableView 固定section 随着cell滚动而滚动
实现UITableView 的下面这个方法, #pragma mark - Scroll - (void)scrollViewDidScroll:(UIScrollView *)scrollView{ CGFloat sectionHeaderHeight = 40; //固定section 随着cell滚动而滚动 if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) { scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0); } else if (scrollView.contentOffset.y>=sectionHeaderHeight) { scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0); } }
实际项目中实现:
/frame 自定义head - (void)scrollViewDidScroll:(UIScrollView *)scrollView { dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{ if ([self.delegate respondsToSelector:@selector(tableViewDidScroll:)]) { [self.delegate tableViewDidScroll:scrollView]; } }); if (isAnCompletion) { return; } [self.listTableView updateTimeLabel]; if (lastContentOffset < scrollView.contentOffset.y) { //向上滚动 //head //如果导航在中间 if (self.fsofViewController.navView.frame.origin.y > 64) { // self.fsofViewController.navView.hidden = YES; [UIView animateWithDuration:0.5 animations:^{ isAnCompletion = YES; self.fsofViewController.navView.alpha = 0.3; self.fsofViewController.navView.alpha = -self.fsofViewController.headView.frame.origin.y/(self.fsofViewController.headView.frame.size.height-self.listuserNavbarHeight); // self.fsofViewController.headView.alpha =0; //导航条透明度 self.fsofViewController.barBgView.alpha = -self.fsofViewController.headView.frame.origin.y/(self.fsofViewController.headView.frame.size.height-self.listuserNavbarHeight); self.fsofViewController.headView.frame = CGRectMake(0, -self.listuserHeadHeight+self.listuserNavbarHeight, self.fsofViewController.headView.frame.size.width, self.fsofViewController.headView.frame.size.height); //导航 self.fsofViewController.navView.frame = CGRectMake(0, self.listuserNavbarHeight, self.fsofViewController.navView.frame.size.width, self.fsofViewController.navView.frame.size.height); self.fsofViewController.headView.alpha =0.5; //所有的list一起变 for (id view in self.fsofViewController.bgScrollView.subviews) { if ([view isKindOfClass:[CommunityList class]]) { CommunityList *cView; cView = view; cView.frame = CGRectMake(cView.frame.origin.x, self.listuserNavbarHeight, self.frame.size.width, [UIScreen mainScreen].bounds.size.height-self.listtabbarHeight-self.listuserNavbarHeight); cView.listTableView.frame = CGRectMake(0, 0, self.frame.size.width, [UIScreen mainScreen].bounds.size.height-self.listtabbarHeight-self.listuserNavbarHeight); } } } completion:^(BOOL finished) { isAnCompletion = NO; self.fsofViewController.headView.alpha =0; // self.fsofViewController.navView.alpha = 1; }]; } }else{ if (scrollView.contentOffset.y <= -1) { //向下滚动 [UIView animateWithDuration:0.5 animations:^{ isAnCompletion = YES; self.fsofViewController.navView.alpha =1; self.fsofViewController.headView.alpha =1; self.fsofViewController.barBgView.alpha =0; self.fsofViewController.headView.frame = CGRectMake(0, 0, self.fsofViewController.headView.frame.size.width, self.fsofViewController.headView.frame.size.height); //导航 self.fsofViewController.navView.frame = CGRectMake(0, self.listuserHeadHeight, self.fsofViewController.navView.frame.size.width, self.fsofViewController.navView.frame.size.height); //所有的list一起变 for (id view in self.fsofViewController.bgScrollView.subviews) { if ([view isKindOfClass:[CommunityList class]]) { CommunityList *cView; cView = view; cView.frame = CGRectMake(cView.frame.origin.x, self.listuserHeadHeight+self.listheadHeight, self.frame.size.width, [UIScreen mainScreen].bounds.size.height-self.listuserHeadHeight-self.listheadHeight-self.listtabbarHeight); cView.listTableView.frame = CGRectMake(0, 0, self.frame.size.width, [UIScreen mainScreen].bounds.size.height-self.listuserHeadHeight-self.listtabbarHeight-self.listheadHeight); } } } completion:^(BOOL finished) { isAnCompletion = NO; }]; } } if(self.fsofViewController.navView.frame.origin.y == 64){ //导航条透明度 [UIView animateWithDuration:0.5 animations:^{ self.fsofViewController.barBgView.alpha = 1.0; }]; }else{ //导航条透明度 self.fsofViewController.barBgView.alpha = 0.0; } } - (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate { } - (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{ lastContentOffset = scrollView.contentOffset.y; }