swift - 如何为 UITableView (Swift) 中的每个 TextField 调用不同的函数?

标签 swift uitextfield tableview action

我有一个 UITableView,我的原型(prototype)单元格由一个标签和一个 TextField 组成。我还有一个包含函数 func1、func2、fun3 的 MyClass 类,...我有几个使用相同 tableViewCell 原型(prototype)的 ViewController。每个 viewController 都有一个 MyClass 的实例,称为 inst1、inst2 和 inst3。当我在 FirstViewController 的 TableView 中输入文本时,我希望每一行都从与该行对应的 MyClass 实例中调用一个函数。

因此,当我在 FirstViewController 的第 1 行中输入文本时,我想将输入到 textField 中的数据传递到 inst1 的 func1 中。当数据输入到 FirstViewController 的第 2 行时,我希望将文本字段中的数据传递到 inst1 的 func2 中。依此类推。

我对此很陌生,非常感谢您帮助我弄清楚如何做到这一点。让我知道这是否没有意义,我可以尝试重新措辞。我真的需要帮助。提前致谢!

*更新问题以显示我的代码

下面是我的代码: FirstViewController.swift

extension FirstViewController: MyCellDelegate {
    func MyCell(_ cell: UITableViewCell, didEnterText text: String) {
        if let indexPath = tableView.indexPath(for: cell) {
            if (indexPath.hashValue == 0) {
                inst1.func1(one: text)
            }
            if (indexPath.hashValue == 1) {
                inst1.func2(two: text)
            }
        }
        totalText.text = inst1.getMyTotal()
    }
}

import UIKit

class FirstViewController: UIViewController,  UITableViewDataSource, UITableViewDelegate {
    let inst1 = MyClass()
    @IBOutlet weak var totalText: UILabel!
    @IBOutlet weak var tableView: UITableView!

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 11
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "myTableCell") as! TableViewCell
        let text = cell.cellData[indexPath.row]
        cell.myTextField.tag = indexPath.row
        cell.delegate = self
        cell.myLabel.text = text
        cell.myTextField.placeholder = text
        return cell
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.dataSource = self
        tableView.delegate = self
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

TableViewCell.swift

import UIKit
protocol MyCellDelegate: class {
    func MyCell(_ cell: UITableViewCell, didEnterText text: String)
}

class TableViewCell: UITableViewCell {
    weak var delegate: MyCellDelegate?

    public var cellData: [String] = ["1","2","3","4","5","6","7","8","9","10","11"]

    @IBOutlet weak var myLabel: UILabel!

    @IBOutlet weak var myTextField: UITextField!

    override func awakeFromNib() {
        super.awakeFromNib()
    }

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

当我在 FirstViewController 扩展中设置断点时,它永远不会运行该代码。

最佳答案

WillDisplayCell 中将标签添加到 UITextField。还创建一个协议(protocol)来通知相应的 viewController 并将自己设置为此处的委托(delegate)。

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
 let cell = tableView.dequeueReusableCell(withIdentifier: "reuseIdentifier")
 cell.textField.tag = indexPath.row
 cell.delegate = self
 }

你的单元类中的协议(protocol)看起来像这样

protocol MyCellDelegate: class {
func MyCell(_ cell: UITableViewCell, didEnterText text: String)
}

class MyCell: UITableViewCell, UITextFieldDelegate {
weak var delegate: MyCellDelegate?
override fun awakeFromNib() {
 super.awakeFromNib()
 textField.delegate = self
 }
//All the remaining code goes here


func textFieldShouldReturn(_ textField: UITextField) -> Bool {   //delegate method
textField.resignFirstResponder()
delegate?.MyCell(self, didEnterText: textField.text! )
return true
}
}

现在再次在符合其委托(delegate)的 FirstViewController 中执行此操作

extension FirstViewController: MyCellDelegate {
 func MyCell(_ cell: UITableViewCell, didEnterText text: String) {
 if let indexPath = tableView.indexPathForCell(cell) {
   // call whichever method you want to call based on index path
  }
 }

关于swift - 如何为 UITableView (Swift) 中的每个 TextField 调用不同的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51073748/

相关文章:

swift - 如何在 swift xcode 中向小于 1.00 的答案开头添加零?

iphone - 使用 UITextField 中的号码调用电话

ios - UITextfield 的 NSDictionary 元素

ios - 内联日期选择器的实现

ios - 测试来自 NSMutableArray 的值并显示到 Tableview

swift - Swift 中来自 URL 的部分组件

swift - UITableView 不显示原型(prototype)单元格

swift - 将类型传递给泛型函数并进行比较

ios - 布局 UITextField 无论清除按钮 X

iphone - 我如何使一个表格 View 指向具有另一个表格 View 的页面?