2010-12-07 76 views
12

我有一個包含UITextField的IB創建的UITableViewCell(帶有關聯的UITableViewCell子類,.m & .h)。此UITextField連接到UITableViewCell子類中的IBOutlet,並且還有一個屬性。在我的表視圖控制器我使用這個自定義單元格用下面的代碼:在自定義UITableViewCell中訪問UITextField

// Customize the appearance of table view cells. 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"textfieldTableCell"]; 
    if (cell == nil) { 
     // Create a temporary UIViewController to instantiate the custom cell. 
     UIViewController *temporaryController = [[UIViewController alloc] initWithNibName:@"TextfieldTableCell" bundle:nil]; 
     // Grab a pointer to the custom cell. 
     cell = (TextfieldTableCell *)temporaryController.view; 
     // Release the temporary UIViewController. 
     [temporaryController release]; 
    } 

    return cell; 

} 

的UITextField,顯示細膩並且點擊時,如預期的鍵盤彈出,但我如何獲得(得到的.text屬性)中的UITextField每行包含?以及如何處理UITextFields的'textFieldShouldReturn'方法?

回答

21

我認爲OP試圖理解的是,一旦用戶將數據輸入到每個字段中,如何訪問UITextField值。在@willcodejavaforfood所建議的單元格創建時這將不可用。

我一直在實現一個窗體並試圖使其儘可能地方便用戶。這是可行的,但請注意,根據您擁有的UITableViewCells/UITextField的數量,它會變得相當複雜。

首先你的問題重新:訪問的UITextField的值:

1)讓您的視圖控制器<UITextFieldDelegate>

2)實現下面的方法:

- (void) textFieldDidEndEditing:(UITextField *)textField { 
    NSIndexPath *indexPath = [self.tableView indexPathForCell:(CustomCell*)[[textField superview] superview]]; // this should return you your current indexPath 

     // From here on you can (switch) your indexPath.section or indexPath.row 
     // as appropriate to get the textValue and assign it to a variable, for instance: 
    if (indexPath.section == kMandatorySection) { 
     if (indexPath.row == kEmailField) self.emailFieldValue = textField.text; 
     if (indexPath.row == kPasswordField) self.passwordFieldValue = textField.text; 
     if (indexPath.row == kPasswordConfirmField) self.passwordConfirmFieldValue = textField.text; 
    } 
    else if (indexPath.section == kOptionalSection) { 
     if (indexPath.row == kFirstNameField) self.firstNameFieldValue = textField.text; 
     if (indexPath.row == kLastNameField) self.lastNameFieldValue = textField.text; 
     if (indexPath.row == kPostcodeField) self.postcodeFieldValue = textField.text; 
    } 
} 

我也使用類似的語法來確保當前編輯的字段是可見的:

- (void) textFieldDidBeginEditing:(UITextField *)textField { 
    CustomCell *cell = (CustomCell*) [[textField superview] superview]; 
    [self.tableView scrollToRowAtIndexPath:[self.tableView indexPathForCell:cell] atScrollPosition:UITableViewScrollPositionMiddle animated:YES]; 
} 

最後,你可以用類似的方式處理textViewShouldReturn:

- (BOOL)textFieldShouldReturn:(UITextField *)textField { 
    NSIndexPath *indexPath = [self.tableView indexPathForCell:(CustomCell*)[[textField superview] superview]]; 
    switch (indexPath.section) { 
     case kMandatorySection: 
     { 
      // I am testing to see if this is NOT the last field of my first section 
      // If not, find the next UITextField and make it firstResponder if the user 
      // presses ENTER on the keyboard 
      if (indexPath.row < kPasswordConfirmField) { 
       NSIndexPath *sibling = [NSIndexPath indexPathForRow:indexPath.row+1 inSection:indexPath.section]; 
       CustomCell *cell = (CustomCell*)[self.tableView cellForRowAtIndexPath:sibling]; 
       [cell.cellTextField becomeFirstResponder]; 
      } else { 
       // In case this is my last section row, when the user presses ENTER, 
       // I move the focus to the first row in next section 
       NSIndexPath *sibling = [NSIndexPath indexPathForRow:kFirstNameField inSection:kOptionalSection]; 
       MemberLoginCell *cell = (MemberLoginCell*)[self.memberTableView cellForRowAtIndexPath:sibling]; 
       [cell.cellTextField becomeFirstResponder]; 
      } 
      break; 
     }   
     ... 
} 
+0

乾杯Rog,我會嘗試一下,當我回到我的蘋果。我想知道Apple如何做到這一點,比如設置應用程序,我以爲我會比這更簡單/更清潔。我想他們可能會用完全不同的方式來做,而且都用代碼。 – Darthtong 2010-12-07 12:18:41

8

在的cellForRowAtIndexPath:包括這個代碼,

yourTextField.tag=indexPath.row+1; //(tag must be a non zero number) 

然後訪問使用

UITextField *tf=(UITextField *)[yourView viewWithTag:tag]; 
0

如果您已經創建了一個類爲您的自定義單元格我建議你反對它的工作文本框。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    MyCustomCell* cell = (MyCustomCell *) [tableView dequeueReusableCellWithIdentifier:@"BDCustomCell"]; 
    if (cell == nil) { 
     // Load the top-level objects from the custom cell XIB. 
     NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"MyCustomCell" owner:self options:nil]; 
     // Grab a pointer to the first object (presumably the custom cell, as that's all the XIB should contain). 
     cell = (MyCustomCell *) [topLevelObjects objectAtIndex:0]; 
    } 

    // This is where you can access the properties of your custom class 
    cell.myCustomLabel.text = @"customText"; 
    return cell; 
} 
2

甚至有更多的解決這兩個問題更簡單的方法,

1.創建一個自定義的細胞的UITableViewCell類,(egtextfieldcell )

2.Now,在textfieldcell.h文件調用textFieldDelegate

3.In的textfieldcell。米文件寫入textFieldDelegate方法 即

-(BOOL)textFieldShouldReturn:(UITextField *)textField; 

-(void)textFieldDidEndEditing:(UITextField *)textField; 
  1. (第一個問題)現在,在
-(BOOL)textFieldShouldReturn:(UITextField *)textField    
{   
     [self.mytextBox resignFirstResponder];   
     return YES;   
} 

5.(第二個問題),

-(void)textFieldDidEndEditing:(UITextField *)textField 
{ 
    nameTextField = mytextBox.text; 
} 

6.C reate自定義委託方法在MaintableViewController

@protocol textFieldDelegate <NSObject> 
-(void)textName:(NSString *)name; 
@end 

7.In MaintableViewController.m文件寫委託方法的實施,

-(void)textName:(NSString *)name{ 
    Nametext = name; 
    NSLog(@"name = %@",name); 
} 

8.call在細胞類委託方法,並傳遞在didendmethod

9.now變量,分配給自cell.delegate,在UITableView的初始化單元時

10.thats它得到了從文本字段傳遞到主視圖的變量,現在做任何你想要的變量

0

This教程對我很有幫助。你可以通過標籤引用你需要的任何對象。

在故事板拖動UIImageViewUITextField等,並將標記設置爲100(無論你想要什麼),然後在你的- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath使用標記來引用它。

這裏的東西你可以做,只記得設置標籤的故事板:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
static NSString *CellIdentifier = @"Cell"; 
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

// Configure the cell... 
if (cell == nil) { 
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
} 

UITextField *tField = (UITextField *)[cell viewWithTag:100]; 

return cell; 
} 
0

這是我設法讓我在斯威夫特定製UITableViewCellUITextField內的文本。我在我的UIButton裏面訪問了另一個自定義UITableViewCell,在我的UITableViewController上有@IBAction。我在我的UITableViewController中只有一個部分,但無論如何這並不重要,因爲您可以自己輕鬆設置和分配此部分。

@IBAction func submitButtonTapped(sender: UIButton) { 
    print("Submit button tapped") 

    let usernameCell = self.tableView.cellForRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 0)) as! UsernameTableViewCell 
    print("Username: \(usernameCell.usernameTextField.text)") 
} 

每當我拍了拍我的UIButton,它給我的文字我UITextField內更新值。