【问题标题】:Get textfield value on Button Click in tableview获取表格视图中按钮单击的文本字段值
【发布时间】:2017-09-16 04:22:31
【问题描述】:

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

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

【问题讨论】:

  • 您没有在cellForRowAtIndexPath 委托中设置按钮标签。应该有yourButton.tag = indexPath.row
  • 是的,我设置了按钮索引
  • 您的按钮在每个单元格中?还是只有一个按钮?
  • 按钮或文本字段位于每个单元格上

标签: ios objective-c uitableview uitextfield


【解决方案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);

【讨论】:

  • 不再工作(空)..还有一个问题是,当滚动表格视图时,表格视图单元格的每个文本字段的值都会发生变化。
  • 为什么你不使用自定义 UITableViewCell 类和 UITextFieldUIButton 就可以了。每次单元格出列时,您都在分配UITextField,这会在此处产生问题。
【解决方案2】:

要以更优雅的方式创建可编辑的UITableViewCell,请查看下图,这将为您提供创建 TableView Cell 所需的所有代码,以响应已完成的操作并打印所选单元格的文本字段的文本。

我会在弄清楚为什么我的 git 帐户无法正常工作后添加代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多