前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >iOS 10 ~ iOS 15 tableview 适配(使用注意事项)

iOS 10 ~ iOS 15 tableview 适配(使用注意事项)

作者头像
清墨
发布2022-11-12 13:44:55
1.9K0
发布2022-11-12 13:44:55
举报
文章被收录于专栏:清墨_iOS分享

一、iOS 11

scrollview,tabelview出现页面偏移问题

image.png

注:iOS11后导航栏和标签栏半透明时才有这样的偏移问题,不透明或者隐藏则没有;

若没有导航栏或标签栏,偏移量为安全顶部距离、安全底部距离

代码语言:javascript
复制
        if #available(iOS 11.0, *) {
            tableView.contentInsetAdjustmentBehavior = .never
        } else {

        }

image.png

image.png

参考:https://www.jianshu.com/p/b42030a37953 https://blog.csdn.net/weixin_34037977/article/details/91634236

二、iOS 14

UITableViewCell里面的view无法响应点击

image.png

原因是cell中contentview改为了懒加载,如果添加自定义子view前没有访问.contentview,添加的view会被contentview覆盖。也是就是view会比contentview提前创建并添加到cell上,导致被contentView挡住

(如果最先有对contentView的访问,则contentView提前被添加,后续添加view不会被挡住)

代码语言:javascript
复制
/// 错误写法
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        addSubview(someView)
    }

我们在使用时应规范写法:contentView.addSubview

代码语言:javascript
复制
/// 规范写法
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        contentView.addSubview(someView)
    }

三、iOS 15

代理、数据源固定存在的代码

代码语言:javascript
复制
     func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 3
    }

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 50
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell: TestTableViewCell
        if let temCell = tableView.dequeueReusableCell(withIdentifier: "1") as? TestTableViewCell {
            cell = temCell
        }else {
            cell = TestTableViewCell.init(style: .default, reuseIdentifier: "1")
        }
        return cell
    }

测试图表(各种cace表现效果,是一个表格链接,不知还能否加载出来) https://g22h5luj8j.feishu.cn/wiki/wikcnv5UZ2xk1kVoOtCZDmqSHRd#doxcn8KAgUQQEGCw2Cu03rpE8rb

汇总结果: 1.当UITableViewStyle为Plain时,如果有设置sectionHeaderView,sectionHeaderView上默认有个22像素的sectionHeaderTopPadding; 如果没有设置sectionHeaderView,则没有这个22像素的sectionHeaderTopPadding 解决方案:

代码语言:javascript
复制
        if #available(iOS 15.0, *) {
            tbView.sectionHeaderTopPadding = 0
        } else {
            // Fallback on earlier versions
        }

2.当UITableViewStyle为Plain时,如果有设置sectionHeaderView或sectionFooterView并且返回的view为nil,上滑或者下滑时,这两个View都会有颜色的变化,颜色与底色有关,但如果返回的view为自定义的View,显示是正常的

3.当UITableViewStyle为Grouped时,不管是sectionHeaderView的sectionHeaderTopPadding还是sectionHeaderView或sectionFooterView返回nil,都是正常显示 (注意Grouped默认的组间距)

四、关于各系统heightForHeaderInSection和heightForFooterInSection返回高度问题

测试图表(各种cace表现效果,是一个表格链接,不知还能否加载出来) https://g22h5luj8j.feishu.cn/wiki/wikcnv5UZ2xk1kVoOtCZDmqSHRd#doxcn2smYAy8GwAyyKCqfewTKi9

注:不实现viewForHeaderInSection,实现heightForHeaderInSection并返回非0高度不生效的原因是iOS7之后sectionHeaderHeight和sectionFooterHeight默认是自动计算高度的,如果想让高度生效,"set to 0 to disable":

代码语言:javascript
复制
    @available(iOS 7.0, *)
    open var estimatedSectionHeaderHeight: CGFloat // default is UITableViewAutomaticDimension, set to 0 to disable

    @available(iOS 7.0, *)
    open var estimatedSectionFooterHeight: CGFloat // default is UITableViewAutomaticDimension, set to 0 to disable

五、UITableViewStyle为Grouped时,tableHeaderView = nil 在各系统表现问题

当代码设置

代码语言:javascript
复制
tableHeaderView = nil
tableHeaderView = UIView()
tableHeaderView = UIView.init(frame: CGRect.zero)
tableHeaderView = UIView.init(frame: CGRect(x: 0, y: 0, width: 0, height: 0))

图例:

image.png

会导致内容向下偏移,这个问题到iOS 13.2苹果才修复(模拟器13.0还有偏移,测试真机13.3已经没了)。这种需求一般是在有无tableHeaderView之间切换,当不需要tableHeaderView时,可如下设置

代码语言:javascript
复制
tableHeaderView = UIView.init(frame: CGRect(x: 0, y: 0, width: 0, height: CGFLOAT_MIN))

测试结果推断出的结论: UITableViewStyle为Grouped时,tableHeaderView = nil 前提条件下 1.没有实现sectionHeader或sectionFooter代理(只实现一个也不行),所有系统下tableView顶部会有一个空白 2.同时实现了sectionHeader和sectionFooter代理,iOS13.2以下系统顶部有空白,iOS13.2及以上系统顶部无留白 3.同时实现了sectionHeader和sectionFooter代理,并且同时设置了sectionHeader和sectionFooter的预估高度为0,所有系统下,顶部都有空白。(设置为0即自动计算高度,设置为非0数值,iOS13.2以下系统顶部有空白,iOS13.2及以上系统顶部无留白)

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-10-29,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、iOS 11
  • 二、iOS 14
  • 三、iOS 15
  • 四、关于各系统heightForHeaderInSection和heightForFooterInSection返回高度问题
  • 五、UITableViewStyle为Grouped时,tableHeaderView = nil 在各系统表现问题
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档