Search code examples
uitableviewfor-loopcell

Why does looping through UITableView's cells only works for the first row?


I am looping through all the cells in my table. Each cell contains a UITextField. I want to validate the data in the text field, but I only get a valid string from the first row of the table. All other subsequent rows return null for its text. Any ideas?

-(IBAction)saveContactToDB:(UIBarButtonItem *)sender
    {
        //TODO  Loop through each row and test the data.
        //      At least one name has to be entered. If this fails a UIAlert is prompted; otherwise, write to DB
        NSLog(@"Attempting to save contact");
        NSInteger contactID;
        for (NSInteger section = 0; section < [_contactInfoTable numberOfSections]; ++section)
        {
            for (NSInteger row = 0; row < [_contactInfoTable numberOfRowsInSection:section]; ++row)
            {
                ContactFieldCell *cell = (ContactFieldCell *)[_contactInfoTable cellForRowAtIndexPath:[NSIndexPath indexPathForRow:section
                                                                                                                         inSection:row]];
                NSLog(@"%@", cell.cellTextField.text);
                NSLog(@"%@", cell.cellTextField.placeholder);

                //Make sure at least one name is entered
                //When i = 0 && j = 0 it means we are on the first row of the name section
                if (section == 0 && row ==0 && cell.cellTextField.text == @"")
                {
                    NSLog(@"Name is required");
                    //Present alert
                    goto ALERTSHOWN;
                }
                else
                {
                    //Write the data to the DB in its respective place
                    if (section == 0)
                    {
                        //Names
                        contactID = [APP.localDatabase saveContactName:cell.cellTextField.text];
                    }
                    else if (section == 1)
                    {
                        //Position
                        [APP.localDatabase saveContactInfoValue:cell.cellTextField.text
                                                       WithType:POSITION
                                                     ForContact:contactID
                                                        AtOrder:row];
                    }
                    else if (section == 2)
                    {
                        //Schedule
                        [APP.localDatabase saveContactInfoValue:cell.cellTextField.text
                                                       WithType:SCHEDULE
                                                     ForContact:contactID
                                                        AtOrder:row];
                    }
                    else if (section == 3)
                    {
                        //Phone number
                        [APP.localDatabase saveContactInfoValue:cell.cellTextField.text
                                                       WithType:PHONE
                                                     ForContact:contactID
                                                         AtOrder:row];
                    }
                    else if (section == 4)
                    {
                        //Miscellaneous
                        [APP.localDatabase saveContactInfoValue:cell.cellTextField.text
                                                       WithType:MISCELLANEOUS
                                                     ForContact:contactID
                                                         AtOrder:row];
                    }
                }
            }
        }
        [self.view removeFromSuperview];
        ALERTSHOWN:;
    }

Solution

  • On getting the cell through the method cellForRowAtIndexPath you transmit as row the variable section and as section the variable row. It should be:

    ContactFieldCell *cell = (ContactFieldCell *)[_contactInfoTable cellForRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:section]];