【问题标题】:How to access to two TextField in a row如何连续访问两个TextField
【发布时间】:2017-08-13 00:37:40
【问题描述】:

我有一个带有动态单元格的UITableView(在以前的视图中是用于创建这些单元格的滑块)。 每行包含两个文本字段。 首先是距离起点。 其次是描述。

我可以通过 tableView 中的单元格访问这些文本字段。 我的表视图:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cellIdentifier = "Cell"
    let cell: FirstAddPointTableViewCell = self.tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! FirstAddPointTableViewCell

    cell.numberOfCell.text? = "\(indexPath.row + 1)."

    cell.distance.text? = arrayTextField1[indexPath.row]
    cell.description.text? = arrayTextField2[indexPath.row]

    cell.distance.tag = indexPath.row
    cell.description.tag = indexPath.row

    cell.distance.delegate = self
    cell.description.delegate = self

    return cell

我的代码中有 func textFieldDidEndEditing,但我不知道如何访问 textFields - 距离和描述以将正确的值保存到我的两个数组中。

我知道这段代码只适用于一个文本字段。如果我有两个文本字段,则此代码是错误的:

func textFieldDidEndEditing(_ textField: UITextField) {
    print("End editing!")

    if textField.text != "" {
       arrayTextField1[textField.tag] = textField.text!
       arrayTextField2[textField.tag] = textField.text!


    } else if textField.text == "" {
        arrayTextField1[textField.tag] = textField.text!
        arrayTextField2[textField.tag] = textField.text!
        }
       }

还有我的 FirstAddPointTableViewCell:

import UIKit

class FirstAddPointTableViewCell: UITableViewCell {

    @IBOutlet weak var cislovaniPrekazek: UILabel!

    @IBOutlet weak var prekazkyFormulare: UITextField!

    @IBOutlet weak var poznamkyFormulare: UITextField!


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

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

我的“想法”类似于这段代码(textFieldDidEndEditing),但我不知道该怎么做。我无法访问它们:

   arrayTextField1[distance.tag] = distance.text!
   arrayTextField2[description.tag] = description.text!

你能帮帮我吗?对不起我的英语。

【问题讨论】:

    标签: ios swift uitableview uitextfield


    【解决方案1】:

    在一次调用 textFieldDidEndEditing() 期间尝试同时设置 arrayTextField1 和 arrayTextField2 似乎不正确。您需要将已编辑的 textField 关联到正确的数组。

    一种想法是在每个 UITextField 的标签中编码额外的信息。您所需要的只是如下所示,我们利用标签是 signed 整数这一事实:

        // In your tableView cellForRowAt method edit the tag setting lines as such:
        cell.distance.tag = indexPath.row + 1 // tag will be 1-based since -0 == 0
        cell.description.tag = -(indexPath.row + 1)  // store it as a negative row so we can distinguish description text fields later
    
        func textFieldDidEndEditing(_ textField: UITextField) 
        {
            guard let text = textField.text else { return }
    
            if textField.tag > 0 {
               arrayTextField1[textField.tag - 1] = text
            } else if textField.tag < 0 {
               arrayTextField2[abs(textField.tag) - 1] = text
            } else {
               assert(true) // some other text field ended editing?
            }
        }
    

    如果你采用上述方法,请务必将标签计算封装在某些函数中,以便将来可维护性更加清晰。

    here 是一种更优雅的解决方案,可以将任意数量的数据附加到 UIView 而不对其进行子类化。

    【讨论】:

    • 感谢您的宝贵时间和帮助。 Assert(0) - 出现错误 - 无法将 Int 类型的值转换为 Bool... 所以我试了一下 - 第一个 textField 没问题,但同一行的第二个是在第三行向下和向上滚动 tableView 之后。跨度>
    • 我更改了断言,现在第二个文本字段(描述)为空。有gif-media.giphy.com/media/l41K5ueOtmfurYa08/giphy.gif
    • 将 UITextFields 存储在一个数组中超出单元格生命周期的整个想法是错误的。在单元格的每次出队后创建它们,并从数据数组中设置它们的文本。我的标签方法仍然可以让您将编辑后的 ​​UITextField 与数据数组相匹配,用于两列。我可以为此编辑我的答案,但实际上它需要一个全新的问题,因为它超出了这个问题的范围,即匹配现有的 textFields 数组。
    • 我的程序是这样工作的:用户在表格视图中填写路线名称、总距离和行数。在此之后 - 点击下一步。然后用户填写从开始的每一行距离(类似于检查点)和文本字段中的描述。因此无法从数组中加载数据,因为它是空的,需要通过 textField 将数据放入数组中。你能给我建议吗?或者问题是我试图从不好的角度解决它?
    • 我知道如果用户首先填写所有距离(检查点) - 保存然后在新表格中查看所有描述。但我认为这对用户来说还有很长的路要走。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-02
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 2016-02-24
    • 1970-01-01
    相关资源
    最近更新 更多