I have 5 cells in a UITableView. Each has a UITextField as a subview, where the user will input data. If I DO use cell reuse, the textfield gets cleared if the cells are scrolled out of view. I don't want to have to deal with this. Is there a way to NOT reuse cells so that I don't have this issue, if so, how?
Is this a bad idea?
I have same feature in one of my apps and I used below code to accomplish that and I never had this kind of problem.
First of all you need to store all your textField value temporary in Array. Make array like this.
arrTemp=[[NSMutableArray alloc]initWithObjects:[NSString stringWithFormat:@""],
[NSString stringWithFormat:@""],
[NSString stringWithFormat:@""],
[NSString stringWithFormat:@""],
[NSString stringWithFormat:@""],
[NSString stringWithFormat:@""],
[NSString stringWithFormat:@""],
[NSString stringWithFormat:@""],
[NSString stringWithFormat:@""],nil];
Then Give all textField tag = indexPath.row;
After that You need to replace textField value in below two methods.
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[arrTemp replaceObjectAtIndex:textField.tag withObject:textField.text];
}
-(void)textFieldDidEndEditing:(UITextField *)textField{
[arrTemp replaceObjectAtIndex:textField.tag withObject:textField.text];
}
At Last You need to set that value in cellForRowAtIndexPath datasource Method. So that whenever user scroll tableview it set previous value from temp array. Like this.
cell.txtEntry.text = [arrTemp objectAtIndex:indexPath.row];
It might possible I forgot some of the code to paste here. So if you have any problem please let me know.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With