在按钮上获取文本框值在桌面视图中单击

问题描述:

我正在开发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

你在你的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

为了更优雅的方式创建一个可编辑UITableViewCell,检查下面的图片这会给你一切为了创造一个TableView中细胞要做过的动作和打印选定单元格的文本框的文本响应所需的代码。

enter image description here

一旦我想通为什么我的git的帐户不工作,我会添加代码。