Reputation: 1137
I've got a tableView
in the ViewController & an array called toDo
, in the TableView I've a cell and in the cell I got textView.
the textView is editable (The user can change the text in the textView).
After the user changes the text - I want the cell to save it to the toDo array, but whenever I reloaddata the text disappears.
Here is what I tried:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: TableViewCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath:indexPath) as! TableViewCell
cell.textField.text = toDo[indexPath.row]
cell.textField.delegate = self
return cell
}
**I have a got a test button that whenever I click it reload data.
Upvotes: 0
Views: 507
Reputation: 773
I think that the problem is that, whenever the system needs to re-render the cell, the method func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath)
is called; that happens before your text view has a chance to save its content in the data model. In your case every time you press the button. I assume you save the content of the text field using optional func textFieldDidEndEditing(_ textField: UITextField)
delegate method. You can add a println("SAVING")
in such method, and a println("RE-RENDERING CELL")
in the tableView(...) method and see the what the sequence of events is. Nots sure if this could help, but I would try that.
Upvotes: 0
Reputation: 38142
Try this out - set the tag on the text field and implement textFieldDidEndEditing:
to update your model before reloading the table view.
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: TableViewCell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath:indexPath) as! TableViewCell
cell.textField.text = toDo[indexPath.row]
cell.textField.tag = indexPath.row
cell.textField.delegate = self
return cell
}
func textFieldDidEndEditing(textField: UITextField) {
toDo[textField.tag] = textField.text
}
Upvotes: 1