如何将 UITextField 连接到 UITableView?

How to connect UITextField to UITableView?

我在一个 ViewController class 中有 2 个 tableView。 在第一个 tableView 中,我在自定义单元格中有 UITextField 和它自己的 UITableViewCell class.

我在 cellForRowAt 内的单元格中显示 textField,但我无法像 Outlet 一样将它连接到 VC 并在 ViewDidLoad 中使用它。

如何使用 VC 内单元格的 textField Outlet?

首先,将 TextField 与 ViewController 连接是个坏主意。

但是如果你想,那么首先你需要检查你 table 是否由静态单元格组成,并且其中包含带有文本字段的所需单元格。然后你可以将 textField 连接到 viewcontroller 的插座并在 viewDidLoad

中使用它

具有动态单元格的 TableView 将在调用 viewDidLoad 后重新加载,因此您不能在该方法中使用文本字段变量

你不应该 link ViewController 中的 TableViewCell 出口 – 你应该有一个单独的 MyTableViewCell class 在那里你 link 所有网点,例如:

class MyTableViewCell: UITableViewCell {
    // MARK: Outlets
    @IBOutlet weak var myTextField: UITextField!
}

您通过函数 tableView:cellForRowAtIndexPath 在视图控制器中引用此 table 视图,您可以在那里处理对单元格执行的所有操作,默认情况下,TextField。

func tableView(_ tableView: UITableView, cellForItemAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withReuseIdentifier: "myCell", for: indexPath) as! MyTableViewCell

    // Customize the cell and the TextField

    return cell
}

把你的插座放在你的手机里 class,当你想访问它的文本时 vc 你会做 let cell = tableview.cellforrow(at indexpath) as! YourCellClass 然后轻松访问文本字段

let text = cell.yourTextField.text

但我建议您删除 tableview,如果它很简单,请改用 scrollview viewcontroller

您不能直接连接 TableCell 中嵌入的任何东西的插座

按照步骤对连接的插座执行代码操作

步骤 1- 创建一个新的 tableViewCell Class 如下 ScreenSHot

步骤 2- 现在将创建的 Class 分配给 Storyboard 中的 TableView 单元格,如下所示

步骤3-单元格class中连接插座的时间通过正常拖动要连接的TF创建单元格class

输出如下

步骤 4- 需要编码

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! CustomTableCell

        cell.fileNameLabel.text = coreClassObject.audioFileName
        cell.durationLabel.text = coreClassObject.audioDuration
        cell.uploadedStatusLabel.text = coreClassObject.audioUploadStatus

        cell.playButton.addTarget(self, action: #selector(playSoundAtSelectedIndex(sender:)), for: .touchUpInside)        
        return cell
    }

重新更新答案以在 ViewDidLoad 中访问 TF

---->我的ViewControllerclass

import UIKit

class tableViewVC: UIViewController
{

    let staticArrayy = ["1","1","1","1","1","1","1","1","1","1"]

    @IBOutlet weak var myDemoTableView: UITableView!

    override func viewDidLoad()
    {
        super.viewDidLoad()

        // Do any additional setup after loading the view.

        ///Set Delegates
        myDemoTableView.delegate = self
        myDemoTableView.dataSource = self

        ///Async operation
        ///To make sure cells are loaded
        DispatchQueue.main.async
            {
            ///Create a Reference TF
            let MyTf : UITextField!

            ///Get Index Path of Which TF is to be Accessed
            let indexPath = IndexPath(row: 0, section: 0)

            ///Create A new cell Reference
            let newCell = self.myDemoTableView.cellForRow(at: indexPath)! as! CustomTableViewCell

            ///Assign Cell TF to our created TF
            MyTf = newCell.cellTF

            ///Perform Changes
            MyTf.text = "Changes text"
        }

    }
}

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

    //Setting cells data
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let cell = self.myDemoTableView.dequeueReusableCell(withIdentifier: "Cell") as! CustomTableViewCell
        cell.cellTF.placeholder = staticArrayy[indexPath.row]
        return cell
    }

    //Setting height of cells
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
    {        
        return 60
    }
}

--->我的手机class

import UIKit

class CustomTableViewCell: UITableViewCell
{

    @IBOutlet weak var cellTF: UITextField!

    override func awakeFromNib()
    {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool)
    {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

----> 我的故事板

---> 模拟器输出