2010-12-07 17 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には、細かい表示され、期待通りにクリックされたときにキーボードがポップアップ表示されますが、どのように私はUITextFieldのアクセス(.textのプロパティを取得)します各行には?また、UITextFieldsの 'textFieldShouldReturn'メソッドをどのように処理するのですか? cellForRowAtIndexPathで

答えて

21

OPが理解しようとしているのは、ユーザーが各フィールドにデータを入力するとUITextField値にアクセスする方法です。これは、@ willcodejavaforfoodによって提案されたようにセルが作成された時点では利用できません。

私はフォームを実装しており、可能な限りユーザーフレンドリーにしようとしています。それは実行可能ですが、あなたが持っているUITableViewCells/UITextFieldの数に応じてかなり混乱することに注意してください。あなたの質問を再にまず

:UITextFieldのの値にアクセスすることは:私も使用

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

1)あなたのビューコントローラが<UITextFieldDelegate>

2)以下のメソッドを実装してください現在の編集されたフィールドが表示されることを確認するのに似た構文です:

- (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、私は私のMacに戻るときにそれを試してみましょう。私は、Appleがこれをやっているのだろうか、設定アプリのように、私はこれよりもはるかに単純明快だと思っていただろう。私は彼らがおそらく完全に異なったやり方で、コードを使ってやると思います。 – Darthtong

8

:このコードが含まれ、

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.CreateセルのカスタムUITableViewCellのクラス、(egtextfieldcell )

2.Now、textfieldcell.hファイルの呼び出しtextFieldDelegate textfieldcell 3.In

インチ

-(BOOL)textFieldShouldReturn:(UITextField *)textField    
{   
     [self.mytextBox resignFirstResponder];   
     return YES;   
} 

5(第2の問題)ですぐMファイル書込みtextFieldDelegate方法 すなわち

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

-(void)textFieldDidEndEditing:(UITextField *)textField; 
  1. (最初の問題)、

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

    6.c

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

    でカスタムデリゲートメソッドをreate 7.In MaintableViewController.mファイルデリゲートセルクラスのメソッド、およびパス

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

    8.call、デリゲートメソッドの実装を書きますdidendmethod

    9.nowにおける変数は、のUITableViewのセルを初期化

    10.thatsをcell.delegateする自己を割り当てますあなたは変数をテキストフィールドからメインビューに渡しました。今では変数で何をしたいのですか?

0

Thisチュートリアルは私にとって参考になりました。必要なオブジェクトをタグで参照することができます。

UIImageViewまたはUITextFieldなどでドラッグし、100にタグを設定してください(- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPathにタグを使用して参照してください)。これは私がスウィフトの私のカスタムUITableViewCellUITextField内のテキストを得ることができた方法です

- (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

:ここ

あなたは、単にストーリーボードにタグを設定することを忘れないで行うことができます何か。私はに@IBActionがある別のカスタムUITableViewCellの中に私のUIButtonの中でこれにアクセスしました。私はUITableViewControllerにセクションが1つしかありませんが、あなたは簡単にこれを設定して割り当てることができるので、とにかく問題はありません。

@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内のテキストの更新値を与えます。

関連する問題