首页 > 解决方案 > 从 TextField 读取输入

问题描述

我正在尝试从多个 TableViewCell 中读取来自 2 个文本字段的输入,看起来它只能访问 input2 并将 input2 两次附加到 inputRead 数组中。我该如何解决这个问题?非常感谢

P/S:这就是我在一个单元格中布局 2 个文本字段的方式:[input1] [input2]

override func tableView(_ tableView: UITableView, cellForRowAt 
indexPath: IndexPath) -> UITableViewCell {
    let customCell = tableView.dequeueReusableCell(withIdentifier:  "CustomCell") as! CustomCell

    customCell.input1.tag = indexPath.row
    customCell.input2.tag = indexPath.row
    customCell.input1.delegate = self
    customCell.input2.delegate = self

    return customCell
} 

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    return textField.endEditing(true)
}

func textFieldDidEndEditing(_ textField: UITextField) {
    let indexPath = IndexPath(row: textField.tag, section: 0)
    if let customCell = self.table.cellForRow(at: indexPath) as? 
    CustomCell {

        let string1 = String(customCell.input1.text!)
        let string2 = String(customCell.input2.text!)
        inputRead.append(input(string1, string2))
    }

}//end class

class CustomCell : UITableViewCell{
    @IBOutlet weak var input1: UITextField!
    @IBOutlet weak var input2: UITextField!
}

标签: iosswiftuitableviewuitextfield

解决方案


推荐阅读