Lovely Lovely - 1 month ago 9
Objective-C Question

Saving data entered in textfield of custom cells

I have a custom cell.In that custom cell,there is a textfield and a label.I actually have made a form using custom cell.The user will enter its name,city,state,country,dob detail in that textfield.Now ,on a click of a button I want to save all this data together in a dictionary.But I am not able to understand that how can I save data for different keys using the same textfield as it is being reused.Please help with some code in objective c.Thanks in advance!

Answer

I am giving you idea, so that you can implement it

Just take a Macro, like this #define textFieldTag 1000

You are re-using the textfField right. So, in cellForRowAtIndexPath (if u r using table view) set the tag of the textField like this: cell.textType.tag = indexPath.row + textFieldTag;

then

access the textField using delegate

- (void)textFieldDidEndEditing:(UITextField *)textField {

switch (textField.tag - textFieldTag) {
    case 0:{
        NSString *name = textField.text;
        //Take a NSMutableDictionary and add the values over here
        break;
    }
    case 1:
    {
        NSString *city = textField.text;

        break;
    }
    case 2:
    {
         NSString *state = textField.text;
        break;
    }
    case 3:
    {
         NSString *country = textField.text;
        break;
    }
    case 4:
    {
         NSString *dob = textField.text;

        break;
    }

    default:
        break;
}
}

And one thing, when you click the save or what ever button u r using to submit the form remember to dismiss the keyboard [textField resignFirstResponder];, otherwise the last value can be nil.

Thank you