• UITableView的style是UITableViewStyleGrouped


    一般情况下,UITableViewStylePlain和UITableViewStyleGrouped是UITableView常用到的style

    之前都是用到的时候,遇到问题直接用度娘,差不多就够用了,今天在修复UI提出的间隙问题,来回改,总觉得没有改到命点。

    下面是UI提出的要求:

     我项目里的代码片段是这样的:

    1. self.tableView = [[UITableView alloc]initWithFrame:(CGRectZero) style:(UITableViewStyleGrouped)];
    2. [self.view addSubview:self.tableView];
    3. [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
    4. make.left.right.bottom.equalTo(@0);
    5. make.top.equalTo(@([UIDevice SafeAreaNaviHeight]));
    6. }];
    7. self.tableView.delegate = self;
    8. self.tableView.dataSource = self;
    9. self.tableView.tag = 10000;
    10. self.extendedLayoutIncludesOpaqueBars = YES;
    11. if (@available(iOS 11.0, *)) {
    12. [self refreshStableTableView:self.tableView];
    13. } else {
    14. #pragma clang diagnostic push
    15. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
    16. self.automaticallyAdjustsScrollViewInsets = NO;
    17. #pragma clang diagnostic pop
    18. }
    19. if (@available(iOS 15.0, *)) {//适配15之后headerHeight会默认22
    20. self.tableView.sectionHeaderTopPadding = 0;
    21. }
    22. self.tableView.contentInset = UIEdgeInsetsMake(7, 0, UIDevice.SafeAreaBottom +49, 0);
    23. self.view.backgroundColor = self.tableView.backgroundColor = [UIColor useLight:HexColor(blk_6) Dark:HexColor(d_blk_6)];
    24. self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

     UITableView用的style是UITableViewStyleGrouped

    也在代理里设置了:

    1. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    2. return [UIView new];
    3. }
    4. //
    5. - (UIView *)tableView:(UITableView *)tableView viewForFoooterInSection:(NSInteger)section{
    6. return [UIView new];
    7. }
    8. //
    9. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    10. return .01;
    11. }
    12. - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    13. return .01;
    14. }

    感觉看着没有啥问题,所以来回试,第一次改的时候是动了个歪脑筋, 

    将tableView的contentInset硬生生的改了一下,看起来大概达到UI的要求了,可能可以骗过UI吧,但是骗不了自己,这个间距并不精确。

    self.tableView.contentInset = UIEdgeInsetsMake(7, 0, UIDevice.SafeAreaBottom +49 - 7, 0);

    一直没有往tableFooterView上想,那是因为我一直以为代理里的viewForFooterInSection和tableView的tableFooterView是一个东西。

    查完之后,大致知道了这两的区别:

    tableFooterView是整个tableView的footerView;

    代理创建的是每个section的footerView。

    UITableView的style是UITableViewStyleGrouped的时候,tableFooterView会有个默认的高度,需要给tableFooterView一个view来实现自己的需求;当设置

    self.tableView.tableFooterView = [UIView new];

    不起作用。因为系统检测到高度为0时,会使用默认高度。所以在我的项目里需要做如下设置才能精确符合UI的要求

    self.tableView.tableFooterView = [[UIView alloc]initWithFrame:(CGRectMake(0, 0, SCREEN_WIDTH, 7))];

    代理里的viewForFooterInSection,如果不设置的话,会有个默认的空白高度,想要去除,或者想要指定的高度,那需要自己去实现代理;

    如果是要去掉每个section的高度则在DataSourse的heightForFooterInSection return 0.01(不要return 0,不然会觉得没有设置高度,变成默认的高度)。为了保险起见同时在Delegate的viewForFooterInSection默认return [UIView new]确保高度设置为0.01成功这样就可以了。

    tableHeaderView同理。

    后期会把遇到的关于UITableView不同的style的不同设置也慢慢记录下来,方便自己理解,省的每次都是好像懂了,又好像还是啥也不懂。

  • 相关阅读:
    STM32 NAND FLASH知识点
    VivifyTech - hackmyvm
    MySQL 开发规范
    【深入理解C】动态内存管理
    java毕业设计计算机组成原理虚拟仿真实验系统(附源码、数据库)
    水利行业评中级职称业绩要求有哪些?帮你分析
    《程序员的七堂课》读书笔记:职业规划
    跨越技术鸿沟,革新存储产业:华瑞指数云重磅发布下一代软件定义存储产品
    竞赛选题 基于机器视觉的二维码识别检测 - opencv 二维码 识别检测 机器视觉
    R语言survminer包的ggsurvplot函数可视化生存曲线、conf.int参数指定添加生存置信区间、conf.int.style指定置信区间的形式
  • 原文地址:https://blog.csdn.net/jdd92/article/details/134376808