Table View Cell 类的视图显示 nil

Posted

技术标签:

【中文标题】Table View Cell 类的视图显示 nil【英文标题】:Table View Cell class's view showing nil 【发布时间】:2021-08-11 11:32:51 【问题描述】:

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

如下所示的 registerCell 类代码

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
   
       
    

【问题讨论】:

请编辑这篇文章并使用“”按钮添加代码。不喜欢代码作为图像。 @Demented07 完成:) 完全愚蠢的问题,但我看不出您是否将@IBOutlet Horizo​​ntalView 连接到寄存器单元格中的视图。 @MacUserT 我做了 :( 我检查了所有的连接 在尝试在测试项目中为您解决此问题之前,我想与您确认一件事。您是否在以文件所有者或UITableViewCell 连接的代码中连接了插座(它必须是UITableViewCell 【参考方案1】:

我对您的horizontalView 出错的唯一猜测是由于IBOutlets 没有以某种方式正确连接(可能会删除插座然后再次更换)但是,我已经让horizontalView 更改它的当用户在textField 内部点击以开始输入/编辑时,拥有自己的背景颜色。

即使没有出现错误,水平视图也没有改变颜色的原因是您没有将文本字段的Delegate 设置为其单元格类。

注意:代理会监听并处理触摸和打字等事件,因此它是您想要的结果所必需的。

我就是这样做的:

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

【讨论】:

非常感谢!!它起作用了:)你能解释一下吗?我是 ios 新手。这就是我所拥有的。为什么这不起作用? let delegatez = RegisterCell() cell.userInfoTxtField.delegate = delegatez 这段代码有什么问题? 您正在创建RegisterCell 的新实例,它没有引用您要更改的背景颜色的UITableViewCell(换句话说,您创建一个新单元格,然后设置文本视图的@ 987654329@ 到该单元格,但该单元格不是要在表格视图中显示的单元格)。我提到的任何你不明白的地方,请尽管问,我会尽力解释。【参考方案2】:

假设您将textField 放在RegisterCell 的contentView 中,您可以将单元格称为textField 的superview 的superview。之后,您可以更改 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)
    

【讨论】:

以上是关于Table View Cell 类的视图显示 nil的主要内容,如果未能解决你的问题,请参考以下文章

如何交互式收缩和增长 Table View Cell?

从 Table View Cell xib 与披露指示符转至情节提要

将数据从 Table View Cell 传递到 IOS 中的 View Controller

iOS Table View Cell 自定义左滑按钮及事件(系统自带方法)

如何让TABLE VIEW中的CELL点击时不显示蓝色背景?

Table Cell 中的 UICollection 视图在 Swift 3 中不显示图像