2012-08-09 51 views
1

我有一個動態表格視圖,它有5個原型單元格,每個單元格內有6個文本區域。我正在標記文本框,但我無法理解如何從「textFieldDidEndEditing」中的所有中獲取值。 在我的代碼,我有這樣的:在動態表格中獲取textfields值

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

問題1: BUT!這隻會讓我從單元格中的一個texfield獲得值...我應該爲每個單元格和texfield使用開關嗎?

問題2:我說,這是一個動態的tableview,因此用戶可以將新聞排(每節)按該左側會出現綠色+按鈕,當他進入提交編輯樣式...和當他進入時,新田的標籤是否有不同的標籤?一方面,我認爲不是,因爲它是新的texfields但是不同的indepaxth.row ....但另一方面我不知道控制器是否需要新的標籤...

回答

2
-(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. 
} 
+0

很棒代碼,謝謝! – Japa 2012-08-09 16:07:03