0
votes

I have a subclass of UITableViewCell that loads a nib on the parent ViewController viewDidLoad event - where the table view is located. There's a UITextView inside this UITableViewCell. The idea is that the user enters text in this textView, and when a button is pressed it will save this text (answers for a questionnaire) in NSUserDefaults.

My problem is that I cannot read the text from the textviews. When the user enters text and the table view is scrolled up and down, I can see the text is kept, so it must be stored in memory somewhere, this happens in cellForRowAtIndexPath. For gathering the text I call this same method but the returned cell is nil. So it seems the text is kept when scrolling but if the method is called programmatically the cell is not returned. I'm not sure how to keep track of this text, I have also tried the method didEndDisplayingCell but sometimes it doesn't get called so it's not reliable.

Nib registration:

UINib *nib = [UINib nibWithNibName:@"QuestionCell" bundle:nil];
[[self tableViewOutlet] registerNib:nib forCellReuseIdentifier:cellIdentifier];

cellForRowAtIndexPath:

QuestionCell *cell = [tableViewOutlet dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell.indexPath){
    cell.indexPath = indexPath;//Setup cell ...

Trying to get the text inside the textView inside the cell (returns nil if not visible):

QuestionCell *cell = (QuestionCell *)[tableViewOutlet cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
1

1 Answers

0
votes

You shouldn't rely on your tableview to retain textfields with the proper text entered. As soon a TableViewCell is scrolled off the screen, it'll get reused by the TableView and the value that the user has entered will likely be overwritten.

Instead, you should register your ViewController as the UITextView's delegate, and update your TableView's datasource object with every key press. Then, you'll be able to read the user's entered text off of that datasource object whenever you need.