gpt4 book ai didi

ios - 在 TableView 中单击按钮获取文本字段值

转载 作者:行者123 更新时间:2023-11-29 11:41:34 27 4
gpt4 key购买 nike

我正在开发 IOS 应用程序。在 TableViewCell 中单击按钮时获取文本字段值。我可以获取按钮的索引并传递给 TableViewCell,然后将单元格传递给文本字段。并从文本字段获取文本以获取(空)文本。但是是问题。请提前致谢。

//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);
}

最佳答案

您在 UITextField 中添加标签作为 _quantity.tag = indexPath.row; 并从 button.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);

关于ios - 在 TableView 中单击按钮获取文本字段值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46250022/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com