Adarkas2302 Adarkas2302 - 4 months ago 84
Swift Question

getting value from uitextfield in uitableview

i have a lot of uitableviewcells with uitextfields inside. And i am catching the editing of the user with the delegate of the textField. That works fine as long as i don't use sections. Because i use the tag of the textField to save the indexPath.row of the cell. The problem is that i have to use sections now and with the sections i would have to save the indexPath.row and .section somehow.

Here is some Code.

func textFieldDidEndEditing(textField: UITextField) {
let indexPath = NSIndexPath(forRow: textField.tag, inSection: 0)
let cell : UITableViewCell? = self.tableView.cellForRowAtIndexPath(indexPath) as UITableViewCell?
if data[(cell?.contentView.viewWithTag(textField.tag + 10000) as! UILabel).text!] != textField.text {
newData[fields[indexPath.row].ID] = textField.text
}
print((cell?.contentView.viewWithTag(textField.tag + 10000) as! UILabel).text)
print(textField.text)
}


Is there a better way to catch the edit of the textfields? Or how could i save both informations in the tag of the textfield?

greetings Adarkas.

Answer

I have resolved my problem like follows.

I have created a unique ID for my Datasource and i use that to determine with cell has been updated.

var row = 0
var section = 0

for var k = 0; k < fieldsWSections.count; k++ {
  for var kk = 0; kk < fieldsWSections[k].count; kk++ {
    if fieldsWSections[k][kk].ID == String(textField.tag) {
      section = k
      row = kk
    }
  }
}

if data[fieldsWSections[section][row].name] != textField.text {
  newData[fieldsWSections[section][row].ID] = textField.text
}

So i don't need to know the indexPath anymore.