简体   繁体   English

内容视图未显示在UITableViewCell上

[英]Content views are not displayed on the UITableViewCell

This is a part of my storyboard: 这是我的故事板的一部分:

图片

this is my running app: 这是我正在运行的应用程序:

图片

This is my part of codes: 这是我的代码部分:

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

I have already connected the button and the textfield in various places and I can guarantee that this part is not wrong, but when I click the Add button in the first row, I only get a cell without any content. 我已经在不同地方连接了按钮和文本字段,可以保证这部分没有错,但是当我单击第一行中的“添加”按钮时,我只会得到一个没有任何内容的单元格。

If I use code like this cell.deleteButton... , Xcode will report an error: 如果我使用像cell.deleteButton...这样的代码,则Xcode将报告错误:

Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value 线程1:致命错误:展开一个可选值时意外地找到零

Then I tried to use the viewWithTag method to see if show the content, but I still get the same error as before. 然后,我尝试使用viewWithTag方法查看是否显示内容,但仍然收到与以前相同的错误。

This is the first time I have encountered this kind of error. 这是我第一次遇到这种错误。 I have no error with similar code and methods in my other programs. 我在其他程序中使用类似的代码和方法也没有错误。

When you configure custom cells inside a storyboard file, you don't need to call register(_:forCellReuseIdentifier:) because the storyboard should have done that for you. 在情节register(_:forCellReuseIdentifier:)文件中配置自定义单元格时,不需要调用register(_:forCellReuseIdentifier:)因为情节register(_:forCellReuseIdentifier:)应该已经为您完成了。

The reason deleteButton is nil is because by re-registering the cell class as you did, you overwrote what the storyboard registered for you. deleteButton为nil的原因是因为通过像您一样重新注册单元格类,您重写了故事板为您注册的内容。 All cells created by dequeueing with that reuse identifier will have no connection to the storyboard and simply be empty. 通过使用该重用标识符出队而创建的所有单元格都不会连接到情节提要,而只是空的。

Assuming all the @IBOutlet s and reuse identifiers and things are set up (which you said you did), then simply dequeue the cell with the reuse identifier set up in storyboard. 假设所有@IBOutlet和重用标识符都已设置(您说过做到了),然后只需使用情节@IBOutlet中设置的重用标识符使单元出队。

Dequeue Cell Example: 出队单元示例:

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

Note: 注意:

Even in cases where you do need to register a class with a table view, you should only have to do this once. 即使在确实需要使用表视图注册类的情况下,也只需要这样做一次。 (For example, during viewDidLoad ) (例如,在viewDidLoad期间)

Even in those times, you should not call it every time you dequeue a cell. 即使在那些时候, 也不应该在每次退出单元格时都调用它。 You're just making your app work harder. 您只是在使您的应用更加努力。

Connecting views to cells in Storyboard 将视图连接到情节提要中的单元格

Set a subclass to the table view 将子类设置为表视图 子类在情节提要中设置为“表格视图”

Set a subclass to the first prototype cell 将子类设置为第一个原型单元 子类设置为原型单元

Set a reuse identifier to the prototype cell 为原型单元设置重用标识符 在此处输入图片说明

Make sure subview ( UIButton , etc.) is connected to property with @IBOutlet (subclass code shown below) 确保子视图( UIButton等)已通过@IBOutlet连接到属性(如下所示的子类代码) IB出口设置为按钮,如单元格所示 IB出口设置为按钮,如按钮所示

Example UITableViewController subclass: 示例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
        }
    }

}

Example UITableViewCell subclass: 示例UITableViewCell子类:

class MyFirstTableViewCell: UITableViewCell {

    @IBOutlet weak var myButton: UIButton!

}

Result: 结果:

模拟器屏幕截图,显示对单元子视图的更改

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM