2017-09-16 71 views
1

我正在開發IOS Application.to在TableViewCell中按鈕單擊時獲取文本框值。我可以得到按鈕的索引並傳遞給TableViewCell,並將其傳遞給textfield.and從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'委託中設置按鈕標籤。應該有'yourButton.tag = indexPath.row' – Torongo

+0

是的,我設置按鈕索引 –

+0

你的按鈕是在每個單元格?或者你只有一個按鈕? – Torongo

回答

1

你在你的UITextField_quantity.tag = indexPath.row;添加標籤,並從button.tag您沒有設置尚未得到指數。您應該在您的按鈕中添加tag

_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)..還有一個問題是當tableview單元格的每個文本字段的scrollview table值發生變化時。 –

+2

爲什麼你不使用自定義的'UITableViewCell'類和'UITextField'和'UIButton'就可以了。您每次在此處創建問題時都會分配「UITextField」。 – Torongo

0

爲了更優雅的方式創建一個可編輯UITableViewCell,檢查下面的圖片這會給你一切爲了創造一個TableView中細胞要做過的動作和打印選定單元格的文本框的文本響應所需的代碼。

enter image description here

一旦我想通爲什麼我的git的帳戶不工作,我會添加代碼。

相關問題