UITableView的style是UITableViewStyleGrouped

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

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

下面是UI提出的要求:

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

objectivec 复制代码
    self.tableView = [[UITableView alloc]initWithFrame:(CGRectZero) style:(UITableViewStyleGrouped)];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(@0);
        make.top.equalTo(@([UIDevice SafeAreaNaviHeight]));
    }];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tag = 10000;
    self.extendedLayoutIncludesOpaqueBars = YES;
    if (@available(iOS 11.0, *)) {
        [self refreshStableTableView:self.tableView];
    } else {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
    self.automaticallyAdjustsScrollViewInsets = NO;
#pragma clang diagnostic pop
    }
    
    if (@available(iOS 15.0, *)) {//适配15之后headerHeight会默认22
        self.tableView.sectionHeaderTopPadding = 0;
    }
    
    self.tableView.contentInset = UIEdgeInsetsMake(7, 0, UIDevice.SafeAreaBottom +49, 0);
    self.view.backgroundColor = self.tableView.backgroundColor = [UIColor useLight:HexColor(blk_6) Dark:HexColor(d_blk_6)];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

UITableView用的style是UITableViewStyleGrouped

也在代理里设置了:

objectivec 复制代码
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
//
- (UIView *)tableView:(UITableView *)tableView viewForFoooterInSection:(NSInteger)section{
    return [UIView new];
}
//
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return .01;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return .01;
}

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

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

objectivec 复制代码
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来实现自己的需求;当设置

objectivec 复制代码
self.tableView.tableFooterView = [UIView new];

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

objectivec 复制代码
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的不同设置也慢慢记录下来,方便自己理解,省的每次都是好像懂了,又好像还是啥也不懂。

相关推荐
小弟调调7 小时前
Vidwall: 支持将 4K 视频设置为动态桌面壁纸,兼容 MP4 和 MOV 格式
macos·swiftui·桌面应用·macos app
Digitally11 小时前
如何将iPhone备份到Mac/MacBook
macos·ios·iphone
帅次11 小时前
【iOS设计模式】深入理解MVC架构 - 重构你的第一个App
ios·swiftui·objective-c·iphone·swift·safari·cocoapods
Frank学习路上1 天前
【IOS】XCode创建firstapp并运行(成为IOS开发者)
开发语言·学习·ios·cocoa·xcode
瓜子三百克1 天前
CALayer的异步处理
macos·ios·cocoa
Fine姐1 天前
传感器WSNs TheDataLinkLayer——B-MAC
macos
九丝城主1 天前
2025使用VM虚拟机安装配置Macos苹果系统下Flutter开发环境保姆级教程--中篇
服务器·flutter·macos·vmware
呆萌的代Ma2 天前
解决Mac上的老版本docker desktop 无法启动/启动后一直转圈/无法登陆账号的问题
macos·docker·eureka
fengyun28912 天前
Omi录屏专家 Screen Recorder by Omi 屏幕录制Mac
macos·mac·录屏·屏幕录制
不学会Ⅳ2 天前
Mac M芯片搭建jdk源码环境(jdk24)
java·开发语言·macos