为什么循环通过UITableView的单元格仅适用于第一行?

时间:2011-08-03 13:18:21

标签: uitableview for-loop cell

我循环遍历表格中的所有单元格。每个单元格包含一个UITextField。我想验证文本字段中的数据,但我只从表的第一行获得一个有效的字符串。所有其他后续行都返回null作为其文本。有什么想法吗?

-(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:;
    }

1 个答案:

答案 0 :(得分:1)

通过方法 cellForRowAtIndexPath 获取单元格时,您将变量部分作为行传输,并将变量作为部分传输。它应该是:

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