UITableView的UITableViewStyleGrouped 和UITableViewStylePlain的区别

  • 一、UITableViewStyleGrouped
注意:去掉头部和中间间隔
正确的理解方法
1.设置标头的高度为特小值 (不能为零 为零的话苹果会取默认值就无法消除头部间距了)
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 0.001)];
view.backgroundColor = [UIColor redColor];
self.tableView.tableHeaderView = view;
2.写代理方法(中间的留白其实是段尾的高度 代理的作用设置段尾的高度 返回值也不能为0)
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
return 0.001;
}

特殊的处理方法也能实现该效果
1.    self.tableView.contentInset = UIEdgeInsetsMake(-44, 0, 0, 0);

2.重写UITableViewHeaderFooterView的
-(void)setFrame:(CGRect)frame{
frame.size.height+=10;
[super setFrame:frame];
}

  • 二、UITableViewStylePlain
1.有多段时 段头停留(自带效果)
2.没有中间的间距和头部间距(要想有的重写UITableViewCell \UITableViewHeaderFooterView里面的setFrame方法)
扩展:让段头不停留(取消粘性效果)
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat sectionHeaderHeight = 30;
    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);
    }
}

注意:
一、tableview 默认顶部增加一段空白高度
这是因为 plain 类型的 UITableView 增加默认的 section 高度,也就是 UITableView 增加了一个新属性:sectionHeaderTopPadding,默认值为automaticDimension,当我们使用UITableViewStylePlain 初始化tableView的时候,sectionHeaderTopPadding 会给 section header 默认增加高度,解决方案是把 sectionHeaderTopPadding 属性设置为0即可:

if (@available(iOS 15.0, *)) {
      self.tableView.sectionHeaderTopPadding = 0.01; // 这个属性,设置为0 时,headerView会随着滑动,给个最小值,就不会滑动了。
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容