ios - 如何从嵌入在 Swift 中的 UITableView 中的文本字段中检索数据?

标签 ios arrays swift uitableview uitextfield

我一直在尝试在 UITableView(即每行中有一个文本字段的 TableView )中创建各种提交表单,但无法从以编程方式生成的文本字段中检索用户输入在 TableView 中。

这是目前的代码:

class VC12: UIViewController, UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate {

@IBOutlet weak var VC12TableView: UITableView!

var VC12Inputs = [String]()

override func viewDidLoad() {
    super.viewDidLoad()
    self.VC12TableView.dataSource = self

    VC12Inputs.append("Input 1")
    VC12Inputs.append("Input 2")
    VC12Inputs.append("Input 3")
}

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = VC12TableView.dequeueReusableCell(withIdentifier: "cell") ?? makeCell()
    let textField = cell.viewWithTag(1) as! UITextField
    let row = indexPath.row

    textField.placeholder = VC12Inputs[row]

    return cell
}

func makeCell() -> UITableViewCell {
    let textField = UITextField(frame: CGRect.zero)

    textField.tag = 1
    [textField].forEach {
        $0.translatesAutoresizingMaskIntoConstraints = false
        $0.borderStyle = .roundedRect
    }

    let cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
    [textField].forEach { cell.contentView.addSubview($0) }

    let views = ["textField": textField]
    let c1 = NSLayoutConstraint.constraints(withVisualFormat: "H:[textField]", options: .alignAllLastBaseline, metrics: nil, views: views)
    let c2 = NSLayoutConstraint(item: textField, attribute: .left, relatedBy: .equal, toItem: cell.layoutMarginsGuide, attribute: .left, multiplier: 1, constant: 0)
    let c3 = NSLayoutConstraint(item: textField, attribute: .centerY, relatedBy: .equal, toItem: cell.contentView, attribute: .centerY, multiplier: 1, constant: 0)
    let c4 = NSLayoutConstraint(item: textField, attribute: .width, relatedBy: .equal, toItem: cell.contentView, attribute: .width, multiplier: 0.9, constant: 0)
    let c5 = NSLayoutConstraint(item: textField, attribute: .height, relatedBy: .equal, toItem: cell.contentView, attribute: .width, multiplier: 0.09, constant: 0)

    NSLayoutConstraint.activate(c1 + [c2, c3, c4, c5])

    return cell
}

@IBAction func VC9Done(_ sender: Any)
{

}

这会生成一个带有 3 个文本字段的标准 TableView ;每行一个,即像这样:/image/JOwO0.png

但是,当用户点击“完成”时,我不确定如何检索用户输入到文本字段中的文本。关于解决此问题的最佳方法有什么想法吗?

此处显示的代码基于 B B ( https://stackoverflow.com/users/2538939/code-different?tab=profile ) 的问题 ( Adding a text field to a UITableview Cell) 的代码不同 (https://stackoverflow.com/users/6155520/b-b) 的 Stack Overflow 答案。

最佳答案

你可以试试这个

class VC12: UIViewController, UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate {

@IBOutlet weak var VC12TableView: UITableView!

var VC12Inputs = [String]()

override func viewDidLoad() {
    super.viewDidLoad()
    self.VC12TableView.dataSource = self

    VC12Inputs.append("Input 1")
    VC12Inputs.append("Input 2")
    VC12Inputs.append("Input 3")
}

func textFieldDidEndEditing(_ textField: UITextField) {
    VC12Inputs[textField.tag] = textField.text! //to update arr with latest value 
}

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

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = VC12TableView.dequeueReusableCell(withIdentifier: "cell") ?? makeCell()
    let textField = cell.viewWithTag(1) as! UITextField
    let row = indexPath.row
    textField.tag = row      //**set tag value here** 
    textField.delegate = self   //**set delegate**

    textField.placeholder = VC12Inputs[row]

    return cell
}

func makeCell() -> UITableViewCell {
    let textField = UITextField(frame: CGRect.zero)

    textField.tag = 1 //don't set tag value here or set with indexpath.item
    . .....
    .....

}

@IBAction func VC9Done(_ sender: Any)
{
    print(VC12Inputs) //here you'll get all updated value at any time 
}

关于ios - 如何从嵌入在 Swift 中的 UITableView 中的文本字段中检索数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48552643/

相关文章:

arrays - swift 错误 "Cannot subscript a value of type [Uint8]"

ios - 如何检查按钮的文本是否包含数组元素?

ios - 无法更新 cocoapods

ios - 如何检测 iOS 应用程序是否正在越狱手机上运行?

ios - AVCameraInput - 将相机从前切换到后时崩溃

javascript - 计算二维数组中的行数

ios - 如何使 UITableViewCell 居中,约束无法对齐

c# - C# 字节数组中的 RAWINPUT RAWHID 结构

iOS:快速将 UnsafeMutablePointer<Int8> 转换为字符串?

swift - tableView.beginUpdates() 行为异常