【发布时间】: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