【问题标题】:UITableView and a fix headerUITableView 和一个修复头
【发布时间】:2026-02-17 03:45:02
【问题描述】:

我有一个唾液观点。主视图是表视图。我想在表格视图上方放置一个固定标签(如固定标题)。我怎样才能在情节提要中做到这一点?

【问题讨论】:

  • 将视图拖到 tableView 的顶部,Interface Builder 会将其插入为tableViewHeader
  • 您是否想要一个在滚动时保持当前事件的固定标题?或者正如@alexburtnik 所说,只是一个整体表头?
  • 我需要一个固定的标题,当我滚动时保持不变。

标签: ios uitableview cocoa-touch


【解决方案1】:

在 Table View Controller 中无法添加固定部分。但是您可以在其中使用视图控制器和表视图。使用此解决方案,您可以在顶部、底部或表格视图上设置固定拍子。

  1. 首先将视图控制器添加到情节提要
  2. 将 tableview 添加到视图控制器
  3. 在检查器中,表格视图 > 设置原型 Cell = 1
  4. 在检查器中,表格视图单元格 > 设置标识符 = 单元格
  5. 添加一个 viewcontroller.swift 并在上面写下这段代码,如下所示

导入 UIKit

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {

@IBOutlet weak var tableView: UITableView!

var arrayTest : [String] = []

override func viewDidLoad() {
    super.viewDidLoad()

    arrayTest = ["iPad","iPhone","iPod","macOS","iOS"]

    tableView.delegate = self
    tableView.dataSource = self

    tableView.reloadData()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return arrayTest.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

    cell.textLabel?.text = arrayTest[indexPath.row]

    return cell
}

}

  1. 在此步骤中,您的表格设置为视图控制器,您可以在表格视图顶部添加视图并在视图上添加标签。

现在您在表格视图的顶部有了一个固定的标题。 您可以在这张图片中看到所有步骤的图片。好好享受。 All image of toturial

【讨论】:

  • 这正是我需要的行为!我要尽快试试这个。谢谢