如何获取UITableViewCell中的textField的值到textFieldDelegate方法shouldChangeCharactersIn中?

时间:2019-02-02 10:47:32

标签: ios swift uitableview uitextfield cell

我有一个带textFields的tableViewCell。在文本字段中更改字符时,我想获取单个textField的值。那么我如何获取所有文本字段的txtfield值

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {


    var arrChanged : NSMutableArray = []


    for i in 0..<arrCheckList.count{
        let indexPath = IndexPath(row: i, section: 0)
        let cell: CheckInCell = tblViewCheckIn.cellForRow(at: indexPath) as! CheckInCell

        if(textField.text?.count != 0){
            let dictData = (arrCheckList.object(at: cell.txtQty.tag) as! NSDictionary).mutableCopy() as! NSMutableDictionary
            dictData.setValue(cell.txtQty.text, forKey: "check_list_qty")

            arrChanged.add(dictData)
        }

    }

    arrCheckList = arrChanged

    return true
}

1 个答案:

答案 0 :(得分:1)

使用以下代码获取应更改字符的文本字段中的完整文本:

 var str:NSString = textField.text! as NSString
 str = str.replacingCharacters(in: range, with: string) as NSString

要获取特定的文本字段,可以在单元格中为行设置标签,在此处可以检查文本字段标签以识别文本字段。

在行集的单元格中:

    cell.textField.tag = indexPath.row

然后应在范围内更改字符

  array[textfield.tag] = str //which is the textfieldvalue

因此,您可以在此处按表视图行索引存储文本字段值。