我正在開發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);
}
您沒有在'cellForRowAtIndexPath'委託中設置按鈕標籤。應該有'yourButton.tag = indexPath.row' – Torongo
是的,我設置按鈕索引 –
你的按鈕是在每個單元格?或者你只有一個按鈕? – Torongo