iOS section 随tableview一起滚动

@interface YGSectionHeaderView : UIView

@property NSUInteger section;

@property (nonatomic, weak) UITableView *tableView;

@end
@implementation YGSectionHeaderView



- (void)setFrame:(CGRect)frame{

    CGRect sectionRect = [self.tableView rectForSection:self.section];

    CGRect newFrame = CGRectMake(CGRectGetMinX(frame),

                                 CGRectGetMinY(sectionRect),

                                 CGRectGetWidth(frame),

                                 CGRectGetHeight(frame));

    [super setFrame:newFrame];

}



@end

 

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

{

    YGSectionHeaderView *sectionHead = [[YGSectionHeaderView alloc] init];

    sectionHead.backgroundColor = [UIColor blueColor];

    sectionHead.section = section;

    sectionHead.tableView = tableView;

    return sectionHead;

}

 

第二种:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView



{



    if ([scrollView isKindOfClass:[UITableView class]])



    {



        CGFloat sectionHeaderHeight = 44;



        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);



        }



    }



}

 

你可能感兴趣的:(tableview)