2011-08-03 40 views
-1

我正在循環表格中的所有單元格。每個單元格包含一個UITextField。我想驗證文本字段中的數據,但我只從表格的第一行獲取有效的字符串。所有其他後續行的文本返回null。有任何想法嗎?爲什麼通過UITableView的單元循環只適用於第一行?

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

我翻轉的值的行和部分中: ContactFieldCell *細胞=(ContactFieldCell *)[_ contactInfoTable的cellForRowAtIndexPath:[NSIndexPath indexPathForRow:部分切入口:行]]; 有趣的錯誤... – crzrcn

回答

1

在通過該方法獲得的細胞的cellForRowAtIndexPath你發送作爲行可變和部分可變。它應該是:

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