我创建了一个自定义表头视图,并以编程方式将其添加到表视图内的表视图控制器中。我设置表格视图滚动时标题视图不会滚动。 此外,对于表视图中的每个部分,还有一个节标题。 问题是当向下滚动时,表标题视图不会隐藏节标题。 在viewdidload中,banner是从UIView继承的自定义类。
- (void)viewDidLoad {
[super viewDidLoad];
self.automaticallyAdjustsScrollViewInsets = NO;
self.tableView.showsVerticalScrollIndicator = NO;
//adjust table view position
self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, HeadImgHeight)];
self.navigationController.navigationBarHidden = YES;
[self.navigationController setToolbarHidden:NO];
//add table header view
self.banner = [[Banner alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, HeadImgHeight)];
self.banner.offsetPoint = CGPointMake(ScreenWidth/2, HeadImgHeight-20);
[self.tableView addSubview:self.banner];
另外,由于我希望滚动时标题视图不会消失,我实现了scrollViewDidScrll函数
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
CGFloat offsetY = scrollView.contentOffset.y;
if (offsetY < 0) {//scroll down
self.banner.frame = CGRectMake(0, 0, ScreenWidth, HeadImgHeight);
self.banner.offsetPoint = CGPointMake(ScreenWidth/2, self.banner.frame.size.height-20);
[self.banner.MonthInComeLabel setFrame:CGRectMake(ScreenWidth/4 - TITLE_WIDTH/2, HeadImgHeight-MONTH_ICON_BOTTOM_MARGIN-TOTAL_OUT_HEIGHT, TITLE_WIDTH, TITLE_HEIGHT)];
[self.banner.MonthOutComeLabel setFrame:CGRectMake(ScreenWidth*3/4 - TITLE_WIDTH/2, HeadImgHeight-MONTH_ICON_BOTTOM_MARGIN-TOTAL_OUT_HEIGHT, TITLE_WIDTH, TITLE_HEIGHT)];
[self.banner.MonthInDetailLabel setFrame:CGRectMake(ScreenWidth/4 - TITLE_WIDTH/2, HeadImgHeight-MONTH_DETAIL_BOTTOM_MARGIN-TOTAL_OUT_HEIGHT, TITLE_WIDTH, TITLE_HEIGHT)];
[self.banner.MonthOutDetailLabel setFrame:CGRectMake(ScreenWidth*3/4 - TITLE_WIDTH/2, HeadImgHeight-MONTH_DETAIL_BOTTOM_MARGIN-TOTAL_OUT_HEIGHT, TITLE_WIDTH, TITLE_HEIGHT)];
}else{
if (offsetY<=30) {
self.banner.frame = CGRectMake(0, self.tableView.contentOffset.y, ScreenWidth, HeadImgHeight-offsetY);
self.banner.offsetPoint = CGPointMake(ScreenWidth/2, self.banner.frame.size.height-20);
[self.banner.MonthInComeLabel setFrame:CGRectMake(ScreenWidth/4 - TITLE_WIDTH/2, HeadImgHeight-MONTH_ICON_BOTTOM_MARGIN-TOTAL_OUT_HEIGHT-offsetY, TITLE_WIDTH, TITLE_HEIGHT)];
[self.banner.MonthOutComeLabel setFrame:CGRectMake(ScreenWidth*3/4 - TITLE_WIDTH/2, HeadImgHeight-MONTH_ICON_BOTTOM_MARGIN-TOTAL_OUT_HEIGHT-offsetY, TITLE_WIDTH, TITLE_HEIGHT)];
[self.banner.MonthInDetailLabel setFrame:CGRectMake(ScreenWidth/4 - TITLE_WIDTH/2, HeadImgHeight-MONTH_DETAIL_BOTTOM_MARGIN-TOTAL_OUT_HEIGHT-offsetY, TITLE_WIDTH, TITLE_HEIGHT)];
[self.banner.MonthOutDetailLabel setFrame:CGRectMake(ScreenWidth*3/4 - TITLE_WIDTH/2, HeadImgHeight-MONTH_DETAIL_BOTTOM_MARGIN-TOTAL_OUT_HEIGHT-offsetY, TITLE_WIDTH, TITLE_HEIGHT)];
}else{
self.banner.frame = CGRectMake(0, self.tableView.contentOffset.y, ScreenWidth, HeadImgHeight-BANNER_FLEXIBLE_LENGTH);
self.banner.offsetPoint = CGPointMake(ScreenWidth/2, self.banner.frame.size.height-20);
[self.banner.MonthInComeLabel setFrame:CGRectMake(ScreenWidth/4 - TITLE_WIDTH/2, HeadImgHeight-MONTH_ICON_BOTTOM_MARGIN-TOTAL_OUT_HEIGHT-BANNER_FLEXIBLE_LENGTH, TITLE_WIDTH, TITLE_HEIGHT)];
[self.banner.MonthOutComeLabel setFrame:CGRectMake(ScreenWidth*3/4 - TITLE_WIDTH/2, HeadImgHeight-MONTH_ICON_BOTTOM_MARGIN-TOTAL_OUT_HEIGHT-BANNER_FLEXIBLE_LENGTH, TITLE_WIDTH, TITLE_HEIGHT)];
[self.banner.MonthInDetailLabel setFrame:CGRectMake(ScreenWidth/4 - TITLE_WIDTH/2, HeadImgHeight-MONTH_DETAIL_BOTTOM_MARGIN-TOTAL_OUT_HEIGHT-BANNER_FLEXIBLE_LENGTH, TITLE_WIDTH, TITLE_HEIGHT)];
[self.banner.MonthOutDetailLabel setFrame:CGRectMake(ScreenWidth*3/4 - TITLE_WIDTH/2, HeadImgHeight-MONTH_DETAIL_BOTTOM_MARGIN-TOTAL_OUT_HEIGHT-BANNER_FLEXIBLE_LENGTH, TITLE_WIDTH, TITLE_HEIGHT)];
}
}
}
[无滚动时的屏幕截图] [1]
[向下滚动屏幕截图,没问题] [2]
screen shot when scroll up, as you can see the section header is over the table header view
有什么想法吗?