【问题标题】:Content views are not displayed on the UITableViewCellUITableViewCell 上不显示内容视图
【发布时间】:2018-05-22 01:34:28
【问题描述】:

这是我故事板的一部分:

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

这是我的部分代码:

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会报错:

线程 1:致命错误:打开包装时意外发现 nil 可选值

然后我尝试使用viewWithTag方法查看是否显示内容,但我仍然得到和以前一样的错误。

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

【问题讨论】:

  • 你能显示deleteButton的代码吗?

标签: ios swift uitableview cell viewwithtag


【解决方案1】:

当您在情节提要文件中配置自定义单元格时,您无需调用 register(_:forCellReuseIdentifier:),因为情节提要本应为您完成。

deleteButton 为 nil 的原因是,通过像您一样重新注册单元类,您覆盖了情节提要为您注册的内容。使用该重用标识符通过出队创建的所有单元格将与情节提要无关,并且只是空的。

假设所有@IBOutlets 和重用标识符和事物都已设置(你说你做了),然后简单地使用情节提要中设置的重用标识符将单元格出列。

出列单元示例:

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 {
        ...
    }
}

注意:

即使您确实需要使用表格视图注册一个类,您也应该只需要这样做一次。 (例如viewDidLoad期间)

即使在那个时候,您不应该在每次出列单元格时调用它。你只是让你的应用更努力地工作。

将视图连接到 Storyboard 中的单元格

为表格视图设置一个子类

为第一个原型单元设置一个子类

为原型单元设置重用标识符

确保子视图(UIButton 等)通过@IBOutlet 连接到属性(子类代码如下所示)

示例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!

}

结果:

【讨论】:

  • 我这样做了,但得到一个错误:*** 由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无法使具有标识符 SubTextFieldCell 的单元格出列 - 必须注册一个 nib 或一个类标识符或连接故事板中的原型单元',您可以看到我的另一个问题:stackoverflow.com/questions/50445191/…
  • 这说明你没有正确设置重用标识符。我附上了一张快照,向您展示在哪里进行设置。
  • 对于您在情节提要中创建的每种类型的单元格,设置不同的Identifier,如我的屏幕截图所示。然后使用相同的字符串将代码中的单元格出列。
  • 谢谢,不过我在storyboard里设置了Identifier,检查了很多遍,还有没有其他错误的可能?
  • 您是否将单元格的按钮和文本字段连接到单元格子类中的@IBOutlets 并将该子类设置为情节提要中的单元格? (看看你的另一个问题,你说你正在使用标签。)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多