I struggle a bit to find or think of a way that I can store the textView text.
so later I can pass it back to the server if needed.
I have a TextView Inside a CustomCell UITableViewCell. (I have 2 sections inside my tableview and in each section there is that custom cell with a textView inside.)
I need to somehow pass or save the text that the user have written so I could pull that text and send it somewhere else.. and each time the text inside my "reusable" cell gets reused the text disappears.
I have tried to save it inside my customCell Swift but I don’t think that’s a good idea to save the text inside a customCell file.
any one have idea?
thank you!
2
Answers
While implementing tableView, we should make sure to have some data source that is data driven. What I mean here is to create a model that drives your tableView’s numberOfSections, numberOfRowsInSection etc. This way you can have a property in your model to store textView’s text.
Sample Model:
Using this you can create your dataModel like this:
This will help to update property in model and you can have your data to pass it to backend later.
First step:
Create a model to populate the cells.
Create a list of models (to populate the tableview)
Second step:
This has several way to do so but i chose the delegate approach.
Make your CustomCell conform to UITextViewDelegate.
Save a indexPath var on the CustomCell:
var indexPath: IndexPath?
Implement this UITextview Delegate method on the cell:
func textViewDidChange(textView: UITextView)
Set the textView delegate in the cell awakeFromNib:
}
5.Create a CustomCellDelegate:
Call this function on the cell when needed (probably inside textViewDidChange function implementation on the cell).
Make the viewControlller that holds the tableview to conform to your CustomCellDelegate
Implement the CustomCellDelegate cellTextDidChanges method on your viewControlller
On the cellTextDidChanges implementation you should update the correct model text on the model list you created according to the indexPath.
Remember when the tableview will be populated for the first time the text will be empty.