2017-09-16 13 views
1

私はIOSアプリケーションを開発しています.TopViewCellのボタンクリックでテキストフィールド値を取得しています。私は、ボタンのインデックスを取得し、TableViewCellに渡すことができますし、セルをtextfieldに渡し、テキストフィールドからテキストを取得して(Null)テキストを取得することができます。しかし、問題です。事前に感謝してください。ボタンのテキストフィールド値を取得するテーブルビューをクリック

//Tableviewcell create textfield 

_quantity = [[UITextField alloc]initWithFrame:CGRectMake(770.0f, 10.0f,80.0f, 30.0f)]; 
[_quantity setFont:[UIFont systemFontOfSize:15.0f]]; 
_quantity.delegate =self; 
[_quantity.layer setBorderColor: [[UIColor blackColor] CGColor]]; 
[_quantity.layer setBorderWidth: 1.0]; 
_quantity.textAlignment = NSTextAlignmentCenter; 
_quantity.tag = indexPath.row; 
_quantity.text = obj.productQuantity; 
_quantity.leftViewMode = UITextFieldViewModeAlways; 
[_quantity setAdjustsFontSizeToFitWidth:YES]; 
[cell addSubview:_quantity]; 

_quantitySave = [[UIButton alloc]initWithFrame:CGRectMake(770.0f, 45.0f,80.0f, 20.0f)]; 
_quantitySave.tag = indexPath.row; 
[_quantitySave.layer setCornerRadius:4.0f]; 
[_quantitySave.layer setBorderColor: [[UIColor blackColor] CGColor]]; 
[_quantitySave.layer setBorderWidth: 1.0]; 
_quantitySave.tintColor = [UIColor blackColor]; 
[_quantitySave setTitle:@"Delete" forState:UIControlStateNormal]; 
[_quantitySave addTarget:self action:@selector(saveQuantity:) forControlEvents:UIControlEventTouchDown]; 
[cell addSubview:_quantitySave]; 

-(IBAction)saveQuantity:(id)sender{ 
    UIButton *button = (UIButton*)sender; 
    NSInteger index = button.tag; 

    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0]]; 
    UITextField *textField = (UITextField *)[cell viewWithTag:index]; 
    NSLog(@"%@",textField.text); 
} 
+0

'cellForRowAtIndexPath'デリゲートにbuttonタグを設定しませんでした。 'yourButton.tag = indexPath.row'があるはずです – Torongo

+0

はい私はボタンのインデックスを –

+0

あなたのボタンはすべてのセルに設定していますか?またはボタンは1つだけですか? – Torongo

答えて

1

あなたは_quantity.tag = indexPath.row;としてあなたUITextFieldにタグを追加し、あなたがまだ設定されていませんでしたbutton.tagからインデックスを取得しています。ボタンにtagを追加する必要があります。これはあなたに完了アクションと印刷選択したセルのテキストフィールドのテキストに対応テーブルビューのセルを作成するために必要なすべてのコードを与える、よりエレガントな方法でチェック以下の画像を編集可能なUITableViewCellを作成するには

_quantity = [[UITextField alloc]initWithFrame:CGRectMake(770.0f, 10.0f,80.0f, 30.0f)]; 
    [_quantity setFont:[UIFont systemFontOfSize:15.0f]]; 
    _quantity.delegate =self; 
    [_quantity.layer setBorderColor: [[UIColor blackColor] CGColor]]; 
    [_quantity.layer setBorderWidth: 1.0]; 
    _quantity.textAlignment = NSTextAlignmentCenter; 
    //_quantity.tag = indexPath.row; 
    yourButtonObject.tag = indexPath.row 
    _quantity.text = obj.productQuantity; 
    _quantity.leftViewMode = UITextFieldViewModeAlways; 
    [_quantity setAdjustsFontSizeToFitWidth:YES]; 
    [cell addSubview:_quantity]; 


-(IBAction)saveQuantity:(id)sender{ 
UIButton *button = (UIButton*)sender; 
NSInteger index = button.tag; 

UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0]]; 
UITextField *textField = (UITextField *)[cell viewWithTag:index]; 
NSLog(@"%@",textField.text); 
+0

もう一度動作しません(null)..もう1つの問題は、スクロールテーブルビューがテーブルビューセルの各テキストフィールドの値を変更するときです。 –

+2

カスタム 'UITableViewCell'クラスと' UITextField'と 'UIButton'をなぜ使用していないのですか?ここで問題を発生させるセルのデキュー時に、常に 'UITextField'を割り当てています。 – Torongo

0

enter image description here

私のGitのアカウントが動作していない理由を私は考え出した後、私は、コードを追加します。

関連する問題