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

相關文章
相關標籤/搜索