Search code examples
uitableviewtagsuitextfieldnsindexpath

Get textfields values in dynamic tableview


I have a Dynamic table view which has 5 prototype cells, inside each cell I have 6 textfields. I´m tagging the textfields but I´m having trouble understanding How can I get the values from all of them in the "textFieldDidEndEditing". In my code I have this:

-(void) textFieldDidEndEditing:(UITextField *)textField
{
NSMutableArray *cellOneContentSave = [[NSMutableArray alloc] init];
NSString *cellOneTexfieldOneTxt;
if (textField == [self.view viewWithTag:1503])
{
cellOneTexfield1Txt = textField.text;
[cellOneContentSave addObject:cellOneTexfieldOneTxt];  
}

Problem 1: BUT! this only get´s me the value from one texfield in cell one...Should I be using a switch for each cell and texfield?.

Problem 2: I said it was a dynamic tableview, so the user can insert news rows(per section) pressing the green + button that appears on the left side when he enters the commit edit style...and when he enters, should the tag´s of the newtexfields have different tag´s?. In one hand I think not, because it´s new texfields BUT different indepaxth.row....but in the other hand I don´t know if the controller demands new tag´s...


Solution

  • -(void) textFieldDidEndEditing:(UITextField *)textField
    {
        // assuming your text field is embedded directly into the table view
        // cell and not into any other subview of the table cell
        UITableViewCell * parentView = (UITableViewCell *)[textField superview];
    
        if(parentView)
        {
            NSMutableArray *cellOneContentSave = [[NSMutableArray alloc] init];
            NSString *cellOneTexfieldOneTxt;
    
            NSArray * allSubviews = [parentView subviews];
            for(UIView * oneSubview in allSubviews)
            {
                // get only the text fields
                if([oneSubview isKindOfClass: [UITextField class]])
                {
                    UITextField * oneTextField = (UITextField *) oneSubview;
    
                    if(oneTextField.text)
                    {
                        [cellOneContentSave addObject: oneTextField.text];
                    } else {
                        // if nothing is in the text field, should
                        // we simply add the empty string to the array?
                        [cellOneContentSave addObject: @""];
                    }
                }
            }
        }
    
        // don't forget to actually *DO* something with your mutable array
        // (and release it, in case you're not using ARC), before this method
        // returns.
    }