Table 查看单元格 class 的视图显示为零

Table View Cell class's view showing nil

这是我用于 Table 视图单元格 class RegisterCell class (Table view cell) and this is the code in my controller part 1 (view did load, view did appear functions, cellForRowAt function and error 的代码。 我的目标是让 registerCell 的 class 中的水平视图在每次单击(或输入)相应的文本字段时更改背景颜色。这个错误的问题是什么?非常感谢任何帮助。 这就是我所说的相应的 texfield 和视图 the controller generates 6 of those

registerCell 的代码如下所示 class

import Foundation
import UIKit

class RegisterCell: UITableViewCell, UITextFieldDelegate{
    
    @IBOutlet weak var userInfoLbl: UILabel!
    
    @IBOutlet weak var userInfoTxtField: UITextField!
    
    @IBOutlet weak var horizontalView: UIView!
   
    
    func textFieldDidBeginEditing(_ textField: UITextField){
       horizontalView.backgroundColor = UIColor(red: 0.0, green: 175.0/210.0, blue: 212.0/188.0, alpha: 1)
         }
}

下面显示的 RegisterViewController 代码:

override func viewDidLoad() {
    super.viewDidLoad()
    editButtonColorAndShapes()
    configure()
    self.tableView.separatorColor = .clear;
    self.tableView.delegate = self
    self.tableView.dataSource = self
   


    // Do any additional setup after loading the view.
}


override func viewDidAppear(_ animated: Bool) {
    configure()
    self.tableView.register(RegisterCell.self, forCellReuseIdentifier: "registerCell")
    editButtonColorAndShapes()
   //r
}

 @objc func textFieldDidChange(textfield: UITextField) {
      

        switch textfield.tag {
        case 0:
           firstname  = textfield.text!

        case 1:
            lastname = textfield.text!

        case 2:
           pnumber = textfield.text!

        case 3:
            email  = textfield.text!

        case 4:
            passwrd   = textfield.text!
            textfield.isSecureTextEntry = true

        case 5:
            zipCode = textfield.text!
        default:
            break
        }
        
    registerModell = RegisterModel(fname: firstname, lname: lastname, fhone: pnumber, mail: email, pass: passwrd, zip: zipCode)
    }

extension RegisterViewController: UITableViewDataSource{
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return titles.count
    }
    

    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
       let cell = tableView.dequeueReusableCell(withIdentifier: "registerCell",
                                                for: indexPath) as! RegisterCell
    
       
       
        cell.selectionStyle = .none
        cell.userInfoLbl?.text = titles[indexPath.row]
        cell.userInfoLbl?.textColor = UIColor(named: "aquamarine")
        cell.horizontalView.backgroundColor = UIColor(red: 0.0, green: 175.0/210.0, blue: 212.0/188.0, alpha: 0.25)
        cell.userInfoLbl?.addCharacterSpacing(kernValue: 4.57)
 cell.userInfoTxtField.delegate = delegatez
        cell.userInfoTxtField.tag = indexPath.row
        cell.userInfoTxtField.addTarget(self, action: #selector(textFieldDidChange), for: .editingChanged)
        cell.configure()
        
        
        
        return cell
   
       
    }

假设您将 textField 放在 RegisterCell 的 contentView 中,您可以将单元格称为 textField 的超级视图的超级视图。之后,您可以更改 horizontalView:

的背景颜色
func textFieldDidBeginEditing(_ textField: UITextField) {
    if let cell = textField.superview?.superview as? RegisterCell {
        cell.horizontalView.backgroundColor = UIColor(red: 0.0, green: 175.0/210.0, blue: 212.0/188.0, alpha: 1)
    }
}

我对你的 horizontalView 出错的唯一猜测是由于 IBOutlets 没有以某种方式正确连接(可能删除插座然后重新更换)但是,我已经得到了horizontalView 以在用户点击 textField 内部开始时更改自己的背景颜色 typing/editing。

即使没有错误,水平视图也没有改变颜色的原因是你没有将文本字段的 Delegate 设置到它的单元格 class.

NOTE: The delegate listens out for and handles events such as touching and typing therefore it is required for your desired outcome.

我是这样做的:

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! RegisterCell
    cell.userInfoTxtField.delegate = cell
    return cell
}