滑动删除时,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 的页眉和页脚部分也会滑动 的相关文章

随机推荐