iOS tableView section设置背景颜色§ion禁止悬浮

1、section设置背景颜色

  • 失败案例

刚开始在继承UITableViewHeaderFooterView的自定义sectionHeader内部,设置self.backgroundColor = [UIColor whiteColor];结果依旧显示灰色bash

而后开始在代理方法- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section里,设置footer.backgroundColor,依旧失败spa

  • 成功案例:

在代理方法- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section里设置backgroundView代理

footer.backgroundView = [[UIImageView alloc] initWithImage:[UIImage ehs_imageWithColor:kBackgroundColor_Gray size:CGSizeMake(SCREEN_WIDTH, kSectionFooterHeight)]];
复制代码

2、禁止section的悬浮

  • header悬浮

网上一搜,会出现不少,大体都是在scroll的代理方法里设置tableview.contentInsetcode

- (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)];string

|| 改成:it

_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - Height_NavBar - kTopTailViewHeight) style:UITableViewStyleGrouped];io

相关文章
相关标签/搜索