I have a textfield inside a UITableviewController. I want to get the user entered data after the user has finished typing and moves to the next field. I want to store that information.
Because once I scroll the user entered data disappears.
Any help is appreciated.
if (([indexPath section] == 1) && ([indexPath row] == 2))
{
static NSString *CellIdentifier1 = @"Cell";
GeneralInfocell *cell = (GeneralInfocell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier1];
[[NSBundle mainBundle] loadNibNamed:@"GeneralInfocell" owner:self options:nil];
cell = addresscell;
cell.selectionStyle = UITableViewCellSelectionStyleNone;
addresscell.txtStreet.delegate = self;
addresscell.txtStreet.font = [UIFont fontWithName:@"Arial" size:14];
addresscell.txtStreet.keyboardType = UIKeyboardTypeDefault;
addresscell.txtStreet.returnKeyType = UIReturnKeyNext;
addresscell.txtStreet.backgroundColor = [UIColor whiteColor];
addresscell.txtStreet.autocorrectionType = UITextAutocorrectionTypeNo;
addresscell. txtStreet.autocapitalizationType = UITextAutocapitalizationTypeNone;
addresscell. txtStreet.textAlignment = UITextAlignmentLeft;
addresscell.txtStreet.textColor = [UIColor blackColor];
[addresscell.txtStreet addTarget:self action:@selector(textFieldDidEndEditing:) forControlEvents:UIControlEventEditingDidEnd];
}
In the textFieldDidEndEditing method I have,
- (void)textFieldDidEndEditing:(UITextField *)textField {
UILabel *label = [NSString stringWithFormat:@"%@",[addresscell.txtCity text]];
NSLog(@"label:%@",label);
}
But I don’t get any value.
Thanks
Your
UITextFieldDelegateshould be something like this –Since everything else is wired correctly, you should get a proper value now.
Since cells are reused, you lose data on scrolling. You will need to reset the text field’s value every time the cell is requested. For this, you can maintain a mutable array of strings if there are multiple fields, one for each of the text fields. When a text field is dismissed, save the data entered in the array and restore it next time the cell is requested for.
Let me know if you need any further assistance.
EDIT
tagall the ten text fields 0 to 9.Declare a NSMutableArray iVar in your view controller, say
data, to store the strings for the text fields.In
-viewDidLoad:,In
-textFieldDidEndEditing:,In
-tableView:cellForRowAtIndexPath:,Don’t forget to release
datain thedeallocmethod.