gpt4 book ai didi

ios - 内容 View 不显示在 UITableViewCell 上

转载 作者:行者123 更新时间:2023-11-30 11:28:56 24 4
gpt4 key购买 nike

这是我的 Storyboard的一部分:

Image

这是我正在运行的应用程序:

Image

这是我的代码部分:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 0 {
if indexPath.row == 0 {
return super.tableView(tableView, cellForRowAt: indexPath)
} else {
tableView.register(SubTextFieldCell.self, forCellReuseIdentifier: "SubTextFieldCell")
let cell = tableView.dequeueReusableCell(withIdentifier: "SubTextFieldCell", for: indexPath) as! SubTextFieldCell

// cell.deleteButton.isEnabled = true
// cell.subTextfield.text = "OK"

print("indexPath.row: \(indexPath.row)")

return cell
}
...

我已经在各个地方连接了按钮和文本字段,我可以保证这部分没有错误,但是当我单击第一行的“添加”按钮时,我只得到一个没有任何内容的单元格。

如果我使用这样的代码 cell.deleteButton...,Xcode 将报告错误:

Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

然后我尝试使用viewWithTag方法查看是否显示内容,但仍然得到与之前相同的错误。

第一次遇到这样的错误。我在其他程序中使用类似的代码和方法没有错误。

最佳答案

当您在 Storyboard 文件中配置自定义单元格时,无需调用 register(_:forCellReuseIdentifier:)因为 Storyboard应该已经为你完成了这些。

原因deleteButton为零是因为通过像您一样重新注册单元类,您覆盖了 Storyboard为您注册的内容。通过使用该重用标识符出列而创建的所有单元格将与 Storyboard没有连接,并且只是空的。

假设所有@IBOutlet和重用标识符以及设置(您说的),然后只需使用 Storyboard 中设置的重用标识符使单元出列即可。

使单元出列示例:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.section == 0 {
if indexPath.row == 0 {
return super.tableView(tableView, cellForRowAt: indexPath)
} else {
// Registering again is unnecessary, because the storyboard should have already done that.
// tableView.register(SubTextFieldCell.self, forCellReuseIdentifier: "SubTextFieldCell")
let cell = tableView.dequeueReusableCell(withIdentifier: "SubTextFieldCell") as! SubTextFieldCell

cell.deleteButton.isEnabled = true
cell.subTextfield.text = "OK"

return cell
}
} else {
...
}
}

注意:

即使您确实需要使用 TableView 注册类,您也只需执行一次。 (例如,在 viewDidLoad 期间)

即使在那些时候,您也不应该在每次使单元出队时都调用它。您只是让您的应用程序更加高效地工作。

将 View 连接到 Storyboard 中的单元格

为 TableView 设置子类 Subclass set to Table View in Storyboard

将子类设置为第一个原型(prototype)单元 Subclass set to prototype cell

为原型(prototype)单元设置重用标识符 enter image description here

确保 subview ( UIButton 等)通过 @IBOutlet 连接到属性(子类代码如下所示) IB Outlet set to button, as seen by cell IB Outlet set to button, as seen by button

示例 UITableViewController子类:

class MyTableViewController: UITableViewController {

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

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if indexPath.row == 0 {
let cell = tableView.dequeueReusableCell(withIdentifier: "MyFirstCell", for: indexPath) as! MyFirstTableViewCell

// Configure cell if needed
cell.myButton.setTitle("New Button Text", for: .normal)
cell.myButton.tintColor = .green

return cell
} else {
let cell = tableView.dequeueReusableCell(withIdentifier: "MySecondCell", for: indexPath) as! MySecondTableViewCell

// Configure cell if needed
cell.myTextField.backgroundColor = .red

return cell
}
}

}

示例 UITableViewCell子类:

class MyFirstTableViewCell: UITableViewCell {

@IBOutlet weak var myButton: UIButton!

}

结果:

Simulator screenshot showing changes to cell subviews

关于ios - 内容 View 不显示在 UITableViewCell 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50458597/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com