2010-06-17 64 views
2

我想在組表格視圖單元格中創建文本字段。如果我在單元格中創建一個文本字段,則文本字段在所有分組表格視圖單元格中重疊。我不知道它是如何發生的。所以我想在分組表格視圖單元格中創建一個文本字段(不是所有的單元格)。我怎樣才能做到這一點?有沒有可用的示例代碼?如何在分組表格視圖中創建文本字段iPhone中的單元格

這裏我的示例代碼,

if(indexPath.section == 0) 
{ 
    if(indexPath.row == 0) 
    { 
     UITextField * lastname = [[UITextField alloc] initWithFrame:CGRectMake(120, 5, 150, 35)]; 

     lastname.tag = titleTag2; 

     [cell.contentView addSubview:lastname]; 

     lastname.delegate = self; 

     lastname.returnKeyType = UIReturnKeyDefault; 
    } 
} 

回答

29

以防萬一任何人遇到此問題,這是如何將UITextFields添加到組表。

在的UITableViewController(我假設是表的委託&數據源)實現以下功能:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    static NSString *CellIdentifier = @"CellIdentifier"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; 

     if (indexPath.section == 0) { 

      // Add a UITextField 
      UITextField *textField = [[UITextField alloc] init]; 
      // Set a unique tag on each text field 
      textField.tag = titleTag2 + indexPath.row; 
      // Add general UITextAttributes if necessary 
      textField.enablesReturnKeyAutomatically = YES; 
      textField.autocorrectionType = UITextAutocorrectionTypeNo; 
      textField.autocapitalizationType = UITextAutocapitalizationTypeNone; 
      [cell.contentView addSubview:textField]; 
      [textField release]; 
     } 
    } 

    // Configure the cell... 
    [self configureCell:cell atIndexPath:indexPath]; 

    return cell; 
} 


- (void)configureCell:(UITableViewCell *)theCell atIndexPath:(NSIndexPath *)indexPath { 

    if (indexPath.section == 0) { 

     // Get the text field using the tag 
     UITextField *textField = (UITextField *)[theCell.contentView viewWithTag:titleTag2+indexPath.row]; 
     // Position the text field within the cell bounds 
     CGRect cellBounds = theCell.bounds; 
     CGFloat textFieldBorder = 10.f; 
     // Don't align the field exactly in the vertical middle, as the text 
     // is not actually in the middle of the field. 
     CGRect aRect = CGRectMake(textFieldBorder, 9.f, CGRectGetWidth(cellBounds)-(2*textFieldBorder), 31.f); 

     textField.frame = aRect; 

     // Configure UITextAttributes for each field 
     if(indexPath.row == 0) { 
      textField.placeholder = @"Name"; 
      textField.returnKeyType = UIReturnKeyNext; 
      textField.autocapitalizationType = UITextAutocapitalizationTypeWords; 
     } else if(indexPath.row == 1) { 
      textField.placeholder = @"Email Address"; 
      textField.returnKeyType = UIReturnKeyNext; 
      textField.keyboardType = UIKeyboardTypeEmailAddress; 
     } else { 
      textField.placeholder = @"Password"; 
      textField.returnKeyType = UIReturnKeyDone; 
      textField.secureTextEntry = YES; 
     } 
    }   
} 

很顯然,你仍然需要實現UITextFieldDelegate方法實際處理文本輸入和移動領域之間的第一響應者。

更新:自發布以來,我意識到您可以使用UIControl contentVerticalAlignment屬性,並將其設置爲UIControlContentVerticalAlignmentCenter,在這種情況下,您可以將您的框架設置爲單元格內容視圖的邊界。

+0

我面臨着同樣的問題...這段代碼不適用於我的邏輯 – Siva 2013-10-25 13:39:09

2

對於很好的例子,你可以看到iPhoneCoreDataRecipes樣品。 在這個示例EditingTableViewCell從nib文件加載,但你可以很容易地在代碼中構造它。

0

我認爲丹尼爾解決方案在某些情況下不好。例如,如果tableview節0包含兩行或更多行,則dequeueReusableCellWithIdentifier可能會返回文本字段標記錯誤的單元格。特別是,如果在indexPath行0被創建並且dequeueReusableCellWithIdentifier在創建第3行indexPath時返回該單元格的單元格被放入隊列中,則文本字段的標記將是錯誤的。

相關問題