【问题标题】:How to connect UITextField to UITableView?如何将 UITextField 连接到 UITableView?
【发布时间】:2018-02-05 23:02:40
【问题描述】:

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

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

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

【问题讨论】:

  • 视图控制器不应直接与单元格中的文本字段对话。单元类应该处理文本字段并通过委托或闭包将相关信息传递回您的视图控制器。

标签: ios swift uitableview uitextfield


【解决方案1】:

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

按照步骤在连接插座的情况下执行代码操作

步骤 1- 创建一个新的 tableViewCell 类,如下面的 ScreenSHot

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

第 3 步- 连接单元类中的插座的时间是通过在单元类中正常拖动要连接的 TF 来创建的

输出如下

步骤 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 的答案

---->我的 ViewController 类

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

--->我的细胞类

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
    }

}

---->我的故事板

--->模拟器输出

【讨论】:

  • 我已经这样做了。如何在 ViewDidLoad 我的主 ViewController 中使用单元格中的 textField?
  • 你能解释一下你真正想要实现的流程,以及在TableView加载后需要在didLoad中用TF做什么,/我会为此做示例​​span>
  • 这是我为您制作的,请查看更新后的答案
【解决方案2】:

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

但是,如果您愿意,首先您需要检查您的表格是否由静态单元格组成,并且所需的带有文本字段的单元格在其中。然后你可以将 textField 与 viewcontroller 的 outlet 连接起来,并在 viewDidLoad 中使用

带有动态单元格的 TableView 将在调用 viewDidLoad 后重新加载,因此您不能在该方法中使用 textfield var

【讨论】:

    【解决方案3】:

    您不应该在 ViewController 中链接 TableViewCell 插座 - 您应该有一个单独的 MyTableViewCell 类,您可以在其中链接所有插座,例如:

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

    您通过函数tableView:cellForRowAtIndexPath 在视图控制器中引用此表格视图,您可以在其中处理对单元格执行的所有操作,默认情况下是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
    }
    

    【讨论】:

    • 已经。我不知道如何在主 TableViewController 的 ViewDidLoad 函数中使用这个 textField
    • @IvanKisin 就是这样,你不应该在 TableViewController 的viewDidLoad 中使用这个 TextField。 Apple 以这样一种方式构建 tableView,即您无需在主 ViewController 的 viewDidLoad 中自定义单元格,而是在 cellForItemAt 函数中自定义单元格。为什么需要在viewDidLoad函数中实现呢?
    【解决方案4】:

    将你的插座放在你的 Cell 类中,当你想从 vc 访问它的文本时 你会做let cell = tableview.cellforrow(at indexpath) as! YourCellClass 然后轻松访问文本字段

    let text = cell.yourTextField.text
    

    但我建议你删除 tableview,如果它是简单的 viewcontroller,则使用 scrollview

    【讨论】:

    • 你的问题是什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-01
    • 1970-01-01
    相关资源
    最近更新 更多