美文网首页
UITableView 表头覆盖单元格问题

UITableView 表头覆盖单元格问题

作者: 谢顶强 | 来源:发表于2020-07-23 10:00 被阅读0次

现象描述

UITableView在加载数据完毕后,刷新UI,此时表头覆盖了顶部的几个单元格,导致出现数据显示不完全的现象。由于公司测试设备型号不完全,仅在iPad mini(9.3.5)出现了此现象,其余的设备都可以显示完整的数据

代码分析

在使用UITableView 时,我直接在 UITableView 实例化的时候为其添加 tableHeaderView,并且此时实例化的 tableHeaderView 的frame 值为 zero。

之后,在控制器执行 ViewWilLayoutSubviews 时未 tableView 和 tableHeaderView 设置需要的frame


let tableView:UITableView = {

    let tableView = UITableView(frame:.zero, style: .grouped)

    tableView.backgroundColor = UIColor.bcBackgroundPrimary

    tableView.register(BCUserIntegralTableViewCell.self,

        forCellReuseIdentifier: BCUserIntegralTableViewCellID)

    tableView.separatorStyle = .none

    tableView.showsVerticalScrollIndicator = false

    tableView.showsHorizontalScrollIndicator = false

    tableView.tableHeaderView = UIView(frame:.zero)

}()

override func viewDidLoad() {

    super.viewDidLoad()

    self.view.addSubview(tableView)

}

override func viewWillLayoutSubviews() {

    super.viewWillLayoutSubviews()

    tableView.frame =

        CGRect(x:0,y:0,width:self.view.width,hight:self.view.height)

    tableView.tableHeaderView?.frame =

        CGRect(x:0,y:0,width:tableView.width,height:300)

}

解决方案

经过分析,我认为我们在为 UITableView 在控制器执行 ViewWillLayoutSubViews 之前已经执行了某些可能影响到 tableView 放置单元格区间的操作,而在控制器生成之后没有随着 tableHeaderView 的高度变化而及时变化,所以我决定在执行 ViewWillLayoutSubviews 之后再为 tableView 添加 tableHeaderView,如下


let tableView:UITableView = {

    let tableView = UITableView(frame:.zero, style: .grouped)

    tableView.backgroundColor = UIColor.bcBackgroundPrimary

    tableView.register(BCUserIntegralTableViewCell.self,

        forCellReuseIdentifier: BCUserIntegralTableViewCellID)

    tableView.separatorStyle = .none

    tableView.showsVerticalScrollIndicator = false

    tableView.showsHorizontalScrollIndicator = false

}()

let tableHeader:UIView = UIView(frame:.zero)

override func viewDidLoad() {

    super.viewDidLoad()

    self.view.addSubview(tableView)

}

override func viewWillLayoutSubviews() {

    super.viewWillLayoutSubviews()

    tableView.frame =

        CGRect(x:0,y:0,width:self.view.width,hight:self.view.height)

    tableHeader.frame =

        CGRect(x:0,y:0,width:tableView.width,height:300)

    tableView.tableHeaderView = tableHeader

}

安装到测试机,运行,OK

相关文章

  • UITableView 表头覆盖单元格问题

    现象描述 UITableView在加载数据完毕后,刷新UI,此时表头覆盖了顶部的几个单元格,导致出现数据显示不完全...

  • 2019-11-20

    表头表头单元格单元格单元格单元格

  • md 使用

    图片 控制尺寸 是是是 啊啊啊的 好 头 表头表头单元格单元格单元格单元格

  • IOS开发 UITableView单元格

    UITableView单元格 本节学习内容: 1.UITableView单元格基础 2.UITableView单元...

  • UITableView实用技巧

    1.去除多余的空白单元格,当UITableView的单元格较少,UITableView空白处会出现多余的单元格: ...

  • UITableView

    UITableView 创建UITableView 代码 使用Masonry布局 storyboard动态单元格记...

  • 三个懒人必用的懒人小技巧,新手请拿好

    1.为单元格设置多斜线表头 说到为表格设置斜线表头,大家肯定觉得,那有什么难的! 不就是:选中单元格 - 右键 -...

  • HTML表单和表格

    1.表格: 行 单个单元格 表头单元格 width 表格边框 borderColor 边框颜色...

  • UITableView优化思路与复用

    单元格复用问题 demo 点击此处下载 UITableView最核心的思想就是UITableViewCell的重...

  • UITableView刷新各部分

    UITableView中刷新Cell 有时可能也会遇到刷新UITableView的表头,区头,区脚,但是[UIta...

网友评论

      本文标题:UITableView 表头覆盖单元格问题

      本文链接:https://www.haomeiwen.com/subject/nfeclktx.html