滑动删除时,TableView 的页眉和页脚部分也会滑动

2024-05-01

我的 TableView 有问题。添加节页脚后,我意识到当我滑动删除时它会移动。

我创建了一个仅包含此功能的最小项目来显示我面临的问题。 这是我得到的结果

我有两个 TableViewCell : DetailCell

import UIKit

class DetailCell: UITableViewCell {


@IBOutlet weak var myTextLabel: UILabel!


override func awakeFromNib() {
    super.awakeFromNib()
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)
}

}

和页眉页脚单元格

import UIKit

class HeaderFooterCell: UITableViewCell {
@IBOutlet weak var thisTextLabel: UILabel!

override func awakeFromNib() {
    super.awakeFromNib()
}

override func setSelected(_ selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)
}

}

这是tableViewController

import UIKit

class TableViewController: UITableViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    let statusBarHeight = UIApplication.shared.statusBarFrame.height
    self.tableView.contentInset = UIEdgeInsetsMake(statusBarHeight, 0.0, 0.0, 0.0)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

// MARK: - Table view data source

override func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 5
}


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "myTableViewCell", for: indexPath) as! DetailCell
        switch indexPath.row {
            case 0: cell.myTextLabel?.text = "one - one - one - one"
            case 1: cell.myTextLabel?.text = "two - two - two - two"
            case 2: cell.myTextLabel?.text = "three - three - three - three"
            case 3: cell.myTextLabel?.text = "four - four - four - four"
            case 4: cell.myTextLabel?.text = "five - five - five - five"
            default: break
        }
        return cell
}

override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 44
}
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
    let cell = tableView.dequeueReusableCell(withIdentifier: "headerOrFooterCell") as! HeaderFooterCell
    cell.thisTextLabel.text = "Less"
    return cell
}

override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return 44
}
override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    let cell = tableView.dequeueReusableCell(withIdentifier: "headerOrFooterCell") as! HeaderFooterCell
    cell.thisTextLabel.text = "More"
    return cell
}

// MARK: RowAction for DELETE and MODIFY
override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
    let delete = UITableViewRowAction(style: .destructive, title: "Delete") { (action, indexPath) in
        print ("Delete")
    }

    let modify = UITableViewRowAction(style: .normal, title: "Modify") { (action, indexPath) in
        print("Modify")
    }

    return [delete, modify]
}

}

我是否犯了一个错误,或者有没有办法水平“阻止”页眉和页脚?


将页眉和页脚的类型更改为 UITableViewHeaderFooterView ,然后使用 .dequeueReusableHeaderFooterView(withIdentifier: "headerOrFooterCell") tableView 方法。不要忘记注册笔尖。我测试了下面的代码,它可以正常工作,没有您所写的问题。

import UIKit

class DetailCell: UITableViewCell {
  @IBOutlet weak var myTextLabel: UILabel!
  override func awakeFromNib() {
    super.awakeFromNib()
  }
}



class HeaderFooterCell: UITableViewHeaderFooterView {
  @IBOutlet weak var thisTextLabel: UILabel!

  override func awakeFromNib() {
    super.awakeFromNib()
  }
}


class TableViewController: UITableViewController {

  override func viewDidLoad() {
    super.viewDidLoad()
    let statusBarHeight = UIApplication.shared.statusBarFrame.height
    self.tableView.contentInset = UIEdgeInsetsMake(statusBarHeight, 0.0, 0.0, 0.0)

    self.tableView.register(UINib(nibName:"DetailCell", bundle: nil), forCellReuseIdentifier: "myTableViewCell")
    self.tableView.register(UINib(nibName:"HeaderFooterCell", bundle: nil), forHeaderFooterViewReuseIdentifier: "headerOrFooterCell")
  }

  override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
  }

  // MARK: - Table view data source

  override func numberOfSections(in tableView: UITableView) -> Int {
    return 1
  }

  override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return 5
  }


  override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "myTableViewCell", for: indexPath) as! DetailCell
    switch indexPath.row {
    case 0: cell.myTextLabel?.text = "one - one - one - one"
    case 1: cell.myTextLabel?.text = "two - two - two - two"
    case 2: cell.myTextLabel?.text = "three - three - three - three"
    case 3: cell.myTextLabel?.text = "four - four - four - four"
    case 4: cell.myTextLabel?.text = "five - five - five - five"
    default: break
    }
    return cell
  }

  override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
    return 44
  }
  override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

    let cell = tableView.dequeueReusableHeaderFooterView(withIdentifier: "headerOrFooterCell") as! HeaderFooterCell

    cell.thisTextLabel.text = "Less"
    return cell
  }

  override func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
    return 44
  }
  override func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
    let cell = tableView.dequeueReusableHeaderFooterView(withIdentifier: "headerOrFooterCell") as! HeaderFooterCell
    cell.thisTextLabel.text = "More"
    return cell
  }

  // MARK: RowAction for DELETE and MODIFY
  override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
    let delete = UITableViewRowAction(style: .destructive, title: "Delete") { (action, indexPath) in
      print ("Delete")
    }

    let modify = UITableViewRowAction(style: .normal, title: "Modify") { (action, indexPath) in
      print("Modify")
    }

    return [delete, modify]
  }

}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

滑动删除时,TableView 的页眉和页脚部分也会滑动 的相关文章

  • 在 iOS 上从 GPS 获取时间

    我正在开发一个跟踪器应用程序 该应用程序需要高精度地了解设备位置 即它使用位置服务并忽略水平精度低于 20 米的位置 CLLocation没有明确声明是否通过 GPS 确定 但是 如果水平精度为 20 米或更好 则可以认为它是来自 GPS
  • 在 UIMenuItem 上设置accessibilityLabel

    我正在尝试设置accessibilityLabel of a UIMenuItem而且似乎没有效果 无论如何 VoiceOver 只是读取项目的标题 let foo UIMenuItem title foo action selector
  • 快速球体结合星数据

    我想构建一个观星应用程序 现在我已经构建了一个球体并用星图覆盖它 基于天体坐标 https svs gsfc nasa gov cgi bin details cgi aid 3895 https svs gsfc nasa gov cgi
  • Obj-C / Swift 项目中的致命陷阱异常

    我开始将 Swift 代码集成到我的 Obj C 项目中 一切都进展顺利 但今天 当我更新到 Xcode 6 1 时 事情变得很糟糕 我从之前运行良好的 Swift 代码中收到了许多 陷阱 异常 第一次崩溃位于我的 UIFont 扩展中 这
  • 如何使用 Objective-C 协议

    我需要将 Picker 选择的值继承到其他地方 我正在尝试下面的代码 但空值即将到来 请检查我哪里出错了 我必须继承在中传递的字符串值PickerView 请检查代码 选取器1 h import
  • SpriteKit 碰撞检测中 SKSpriteNode 之间的间隙

    我已经尝试解决这个问题很长一段时间了 我有一个具有简单平台物理原理的游戏 其中玩家跌倒在一个方块上 这可以阻止他跌倒 这是可行的 但是玩家停止的位置和实际对象 精灵节点的位置之间存在明显的差距 这是一个屏幕截图 它应该是不言自明的 clas
  • 测试文本字段中的 double 是否有值

    尝试检查从文本字段获得的双变量是否有值 让值 双倍 Double valueTextfield text if value isEmpty X if 值 nil X 如果 值 0 X 我该怎么做呢 您可以使用 Double 的 init 方
  • ARC 可以与 Core Graphics 对象一起使用吗?

    我最近开始了一个使用自动引用计数 ARC 的新项目 当我分配 CALayer 的内容时 UIView view UIImage image view layer contents image CGImage 我收到一个错误 ARC 不允许将
  • iOS 以编程方式将 AVI 转换为 MP4 格式

    我的应用程序中有一个查询 因为我想将 AVI 格式的视频转换为 MP4 电影格式 所以有没有什么方法可以以编程方式执行此操作 任何代码片段将不胜感激 你需要使用AVAssetExportSession将视频转换为 mp4格式 下面方法转换
  • 使用自动布局约束时如何获取视图的当前宽度和高度?

    我不是在谈论frame属性 因为从中你只能得到xib中视图的大小 我说的是当视图由于其约束而调整大小时 可能在旋转之后 或响应事件 有没有办法获取当前的宽度和高度 我尝试迭代其约束来寻找宽度和高度约束 但这不是很干净 并且在存在内在约束时会
  • 如何在 Swift 中调用 Objective-C 实例类型方法?

    我有一个 Objective C 类 如下所示 interface CustomObjectHavingData NSObject property nonatomic strong NSData objWithData instancet
  • 斯威夫特/iOS。从导航堆栈中删除一些视图控制器

    这是我想做的 但我不确定这是否是正确的方法 所以请给我建议如何去做 我有初始 VC 和导航 VC 我从中推送第一个 VC 从中推送第二个 VC 接下来我介绍 来自第二个 VC 的 NavigationController 第三个 VC 现在
  • 删除 UINavigationBar 下的 1px 边框 - 不起作用

    IBOutlet var navBar UINavigationBar self navBar setBackgroundImage UIImage forBarMetrics UIBarMetrics Default self navBa
  • ios 如何存储用户输入的详细信息并取回?

    How to Store用户输入的详细信息和Get it Back在ios中 我有以下关注TextFields UserName Email Re enter Email id Phone State Address Localityand
  • 准确地从屏幕上的像素获取颜色并转换其颜色空间

    我需要从屏幕上的像素获取颜色并转换其颜色空间 我遇到的问题是 将值与数字色度计应用程序进行比较时 颜色值不相同 create a 1x1 image at the mouse position if let image CGImage CG
  • Swift 中的字典是否应该转换为类或结构?

    我正在开发一个本机 iOS 应用程序 该应用程序从我们也可以控制的 Web 服务接收 JSON 格式的数据 该计划是在大约 18 个月内更换后端数据库 以支持不同的平台 考虑到这一点 我们希望确保 iOS 应用程序能够相对容易地适应新的数据
  • 每次 UIScrollView 释放时都会发生内存泄漏

    在我的应用程序中 我有一个滚动视图和四个表格视图 每次拖动然后释放时 我都会泄漏 48 字节 这确实很重要 正如您所看到的 两组泄漏都有相同的来源 有人见过这样的泄漏吗 Edit 1 当我单击泄漏旁边的箭头时 我会得到泄漏的以下信息 您所看
  • 观察 UIDatePicker 的变化

    我注意到没有委托来观察 UIDatePicker 中的变化 有没有一种方法可以在不确认任何内容的情况下检测选择器中何时进行更改 例如它旋转并落在新数字上的那一刻 我希望能够检测到这一点 我考虑过关键值观察 但我不认为有一个属性会立即改变 您
  • 使用 Swift(使用 SwiftJWT)和 REST API 连接到 Apple Store Connect - 失败并出现 401

    我正在尝试通过他们的 REST API 连接到 Apple Store Connect 虽然这在几天前有效 但我无法弄清楚为什么它停止工作 现在我无法通过身份验证 即我发出的服务器响应的每个请求都是 401 我是否遗漏了什么 我做什么 生成
  • 应用程序被终止时是否会收到 iOS 静默通知

    当发送后台推送时 content available 1 对于被用户杀死的应用程序 该应用程序不会启动到后台模式 并且application didReceiveRemoteNotification fetchCompletionHandl

随机推荐