去掉UItableview headerview黏性(sticky)

#pragma mark - UICollectionViewDelegates for photos
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Main_Screen_Width, 50)];
    headerView.backgroundColor=[UIColor clearColor];
    
    //替换
    UIView *circleView = [[UIView alloc]initWithFrame:CGRectMake(Main_Screen_Width*0.05, 10, 250, 30)];
    [circleView setBackgroundColor:[UIColor clearColor]];
    [headerView addSubview:circleView];
    
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSDate *date = [dateFormatter dateFromString:[newDateArray objectAtIndex:section]];
    
    
    NSDate *today = [NSDate date];
    NSString *strWeek = [NDDateTools getWeekdayFromNSDate:date];
    
    UIFont *myCustomFont1 = [UIFont fontWithName:@"Avenir-Light" size:22];
    UIFont *myCustomFont2 = [UIFont fontWithName:@"Avenir-Light" size:14];
    UIFont *myCustomFont3 = [UIFont fontWithName:@"Avenir-Light" size:16];
    
    int compare = [NDDateTools compareOneDay:today withAnotherDay:date];
    CGFloat topPadding = 10.0f;
    if (compare == 0) {
        UILabel *todayLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, topPadding, 50, 20)];
        todayLabel.text=  @"今天";
        todayLabel.font = myCustomFont1;
        todayLabel.backgroundColor = [UIColor clearColor];
        todayLabel.textAlignment = NSTextAlignmentLeft;
        todayLabel.textColor=RGBCOLOR(98, 98, 98);
        [circleView addSubview:todayLabel];
        
        UILabel *dayLabel = [[UILabel alloc] initWithFrame:CGRectMake(todayLabel.frame.size.width, topPadding+5, 80, 20)];
        NSString *strDay1 = [dateFormatter stringFromDate:date];
        dayLabel.text=  strDay1;
        dayLabel.font = myCustomFont2;
        dayLabel.backgroundColor = [UIColor clearColor];
        dayLabel.textAlignment = NSTextAlignmentCenter;
        dayLabel.textColor=RGBCOLOR(137, 137, 137);
        [circleView addSubview:dayLabel];
        
        UILabel *weekDayLabel = [[UILabel alloc] initWithFrame:CGRectMake(todayLabel.frame.size.width+dayLabel.frame.size.width, topPadding+5, 50, 20)];
        weekDayLabel.text= [NSString stringWithFormat:@"(%@)",strWeek];
        weekDayLabel.font = myCustomFont2;
        weekDayLabel.backgroundColor = [UIColor clearColor];
        weekDayLabel.textColor=RGBCOLOR(137, 137, 137);
        weekDayLabel.textAlignment = NSTextAlignmentCenter;
        [circleView addSubview:weekDayLabel];
    }else{
        
        UILabel *dayLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,topPadding, 120, 20)];
        NSString *strDay = [dateFormatter stringFromDate:date];
        dayLabel.text=  strDay;
        dayLabel.font = myCustomFont1;
        dayLabel.backgroundColor = [UIColor clearColor];
        dayLabel.textAlignment = NSTextAlignmentLeft;
        dayLabel.textColor=RGBCOLOR(98, 98, 98);
        [circleView addSubview:dayLabel];
        
        UILabel *weekDayLabel = [[UILabel alloc] initWithFrame:CGRectMake(dayLabel.frame.size.width,topPadding, 50, 25)];
        weekDayLabel.text= [NSString stringWithFormat:@"(%@)",strWeek];
        weekDayLabel.font = myCustomFont3;
        weekDayLabel.backgroundColor = [UIColor clearColor];
        weekDayLabel.textColor=RGBCOLOR(137, 137, 137);
        weekDayLabel.textAlignment = NSTextAlignmentLeft;
        [circleView addSubview:weekDayLabel];
    }
    
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 50.0;
}


//去掉UItableview headerview黏性(sticky)
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat sectionHeaderHeight = 50;
    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);
    }
}

原文地址:https://www.cnblogs.com/allanliu/p/4425407.html