Search code examples
iosswiftuitableviewuitextfieldcell

How to get value of textField in UITableViewCell into textFieldDelegate Method shouldChangeCharactersIn?


I have a tableViewCell With textFields. I want to get a value for individual textField when character change in textfields. so how can i get txtfield value for all textfields

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
}

Solution

  • Use the following code to get the complete text in text field inside should change characters:

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

    For getting particular text field, you can set tag in cell for row and here you can check the text field tag to identify Text field.

    In cell for row set:

        cell.textField.tag = indexPath.row
    

    Then in should change characters in range

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

    So here you can store text field value as per tableview row index.