一、section设置背景颜色
- 失败案例
刚开始在继承
UITableViewHeaderFooterView的自定义sectionHeader内部,设置self.backgroundColor = [UIColor whiteColor];结果依旧显示灰色然后开始在代理方法
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section里,设置footer.backgroundColor,依旧失败
- 成功案例:
在代理方法- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section里设置backgroundView:
footer.backgroundView = [[UIImageView alloc] initWithImage:[UIImage ehs_imageWithColor:kBackgroundColor_Gray size:CGSizeMake(SCREEN_WIDTH, kSectionFooterHeight)]];
二、禁止section的悬浮
- header悬浮
网上一搜,会出现很多,大致都是在scroll的代理方法里设置tableview.contentInset
- (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);
}
}
- 但是!footer依旧会有问题!然后网上又有header和footer一起禁止的方法
UITableview处理section的不悬浮,禁止section停留的方法,主要是这段代码
-(void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (scrollView == self.tableView)
{
UITableView *tableview = (UITableView *)scrollView;
CGFloat sectionHeaderHeight = kSectionHeaderHeight;
CGFloat sectionFooterHeight = kSectionFooterHeight;
CGFloat offsetY = tableview.contentOffset.y;
HMLog(@"offsetY---%f",offsetY);
if (offsetY >= 0 && offsetY <= sectionHeaderHeight)
{
tableview.contentInset = UIEdgeInsetsMake(-offsetY, 0, -sectionFooterHeight, 0);
}else if (offsetY >= sectionHeaderHeight && offsetY <= tableview.contentSize.height - tableview.frame.size.height - sectionFooterHeight)
{
tableview.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, -sectionFooterHeight, 0);
}else if (offsetY >= tableview.contentSize.height - tableview.frame.size.height - sectionFooterHeight && offsetY <= tableview.contentSize.height - tableview.frame.size.height)
{
tableview.contentInset = UIEdgeInsetsMake(-offsetY, 0, -(tableview.contentSize.height - tableview.frame.size.height - sectionFooterHeight), 0);
}
}
}
但其实还是没有用,依旧会有bug,亲测的方法是改变table的style
- 初始化tableView的时候,指定style ->
UITableViewStyleGrouped
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - Height_NavBar - kTopTailViewHeight)];
|| 改为:
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - Height_NavBar - kTopTailViewHeight) style:UITableViewStyleGrouped];