Jestin Saji Chacko Jestin Saji Chacko - 2 months ago 10
iOS Question

Value On UITextField Changes On Scroll

Before asking this question I have googled a lot and not able to find a suitable answer.

I have a tableView with

Three
sections and
n
number of columns. The no. of rows in each section is also not fixed. The last two columns contains a
UITextField
and initially the value in each textfield is
0.00
. So after entering the Value in each row if I close that section the textField value is revert back to
0.00
. Can anybody tell me how to save this textField value.

Here is my code.

I'm using a custom Cell
GenericTableViewCell
and
self.columnHeaderArray
determines how many columns in tableview. columnHeaderArray Value contains the colum details like the column type, value etc...

in
cellForRowAtIndexPath
:

GenericTableViewCell *cell = nil;
if (cell == nil)
{
cell = [[GenericTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"withColumns:self.columnHeaderArray];

for (GenericTableColumn *theColumn in self.columnHeaderArray)
{
[self processDataForCell:cell forColumn:theColumn atIndexPath:indexPath];
}
return cell;
}
}


processDataForCell
is a method where I updated the column with values and textfield. In this method I check the column type and insert values into each column.

The code for that is.

switch (column.columnType)
{
case textField:
{
UITextField *lblTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, 4, label.frame.size.width - 10, label.frame.size.height - 6)];

lblTextField.delegate = self;

lblTextField.text = theValue; // setting the textField value iniatally 0.00

lblTextField.borderStyle = UITextBorderStyleRoundedRect;
lblTextField.layer.borderWidth = 1.0;
lblTextField.layer.borderColor = [[UIColor blackColor] CGColor];

[label addSubview:lblTextField];

break;
}
}


Any help would be greatly appreciated.

Answer

As you scroll your cell's off view they are deallocated. Each time the cell scrolls back into view it's re-initialized. If you want to retain the value of the textfield I'd save it's contents when you can and hold onto that value.

I have done this before by holding on to a dictionary of Strings that contain the values for my indexPaths. Each time the textfield resigns as first responder I then store the value of the field in my dictionary.

You should register the UITextField's delegate in your view controller and override textFieldDidEndEditing(_:) where you can access the value of the textfield.

Example:

Create a property for the field values:

@property (nonatomic, strong) NSMutableDictionary *textFieldValues;

Override the UITextFieldDelegate method:

-(void) textFieldDidEndEditing: (UITextField *)textField
{
    NSString *key = [NSString stringWithFormat: @"%ld-%ld", (long)indexPath.section, (long)indexPath.row];
    self.textFieldValues[key] = textField.text;
}

Using your above code, assign a tag to the textfield and assign the fields contents using the dictionary:

UITextField *lblTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, 4, label.frame.size.width - 10, label.frame.size.height - 6)];
lblTextField.delegate = self;
lblTextField.tag = indexPath.row;

// pass in the indexPath from cellForRow
NSString *key = [NSString stringWithFormat: @"%ld-%ld", (long)indexPath.section, (long)indexPath.row];
lblTextField.text = self.textFieldValues[key];

lblTextField.borderStyle = UITextBorderStyleRoundedRect;
lblTextField.layer.borderWidth = 1.0;
lblTextField.layer.borderColor = [[UIColor blackColor] CGColor];

[label addSubview:lblTextField];

Hope that helps.

Comments