对网上一些想法做了参考。
第一种方法通过验证觉得不太好(不推荐实用)
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGFloat sectionHeaderHeight = 40;
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);
}
}
第二种方法:通过对自定义sectionView的setFrame的重写,重新设置section的Y值。
#import "ZWSectionHeadView.h"
#define APP_SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width // 屏幕的宽度
#define kHeadView_Height 44
@implementation ZWSectionHeadView
#pragma mark - init
#pragma mark -
+ (ZWSectionHeadView *)showWithName:(NSString *)sectionName
{
ZWSectionHeadView *sectionView = [[ZWSectionHeadView alloc]initWithFrame:CGRectMake(0, 0, APP_SCREEN_WIDTH, kHeadView_Height)];
sectionView.backgroundColor = [UIColor colorWithRed:0.1 green:0.5 blue:0.8 alpha:0.2f];
// title
UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 22, APP_SCREEN_WIDTH - 20, 20)];
titleLabel.textColor = [UIColor blueColor];
titleLabel.text = sectionName;
titleLabel.font = [UIFont systemFontOfSize:16.0f];
[sectionView addSubview:titleLabel];
return sectionView;
}
#pragma mark - sectionHeight
+ (CGFloat)getSectionHeight
{
return kHeadView_Height;
}
#pragma mark - setter
- (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