我在storyboard中使用viewWithTags创建了tableview,在那里我显示了一个数据列表。
为了选择一定数量的药物,我使用UIPickerView,为了分配所选的值,我使用UITextField。
问题是,第一次从UIPickerView中选择一个值时,该值在UITextField中正确显示,但是在我第一次更新它之后,传递给UITextField的值为nil。
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}
- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [NSString stringWithFormat:@"%li",row+1];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
NSLog(@"didSelectRow------%li",row);
selectedQuantity = [NSString stringWithFormat:@"%@",[NSString stringWithFormat:@"%li",row+1]];
NSLog(@"selectedQuantity------%@",selectedQuantity);
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
return [self.quantityOfPicker integerValue];
}
#pragma mark:- UITableView Delegate & Datasource Methods
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 104;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return medicineBrnadArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
BuyhereTableViewCell *buyhereCell = [tableView dequeueReusableCellWithIdentifier:@"BuyhereCell"];
if (buyhereCell==nil) {
buyhereCell = [[BuyhereTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"BuyhereCell"];
}
buyhereCell.backgroundColor = [UIColor colorWithRed:230.0/255.0 green:229.0/255.0 blue:237.0/255.0 alpha:1.0];
UIView *borderView = (UIView *) [buyhereCell viewWithTag:1000];
borderView.backgroundColor = [UIColor whiteColor];
borderView.layer.cornerRadius = 5;
borderView.layer.borderColor = [UIColor lightGrayColor].CGColor;
borderView.layer.borderWidth = 2.0;
[buyhereCell.contentView addSubview:borderView];
UIImageView *imgView = (UIImageView *) [buyhereCell viewWithTag:1001];
imgView.image = [UIImage imageNamed:[buyHereImagesArray objectAtIndex:indexPath.row]];
[buyhereCell.contentView addSubview:imgView];
UILabel *medicineNameLbl = (UILabel *) [buyhereCell viewWithTag:1002];
medicineNameLbl.text = [medicineBrnadArray objectAtIndex:indexPath.row];
[buyhereCell.contentView addSubview:medicineNameLbl];
UILabel *despLabel = (UILabel *) [buyhereCell viewWithTag:1003];
despLabel.text = [medicineDescrpArray objectAtIndex:indexPath.row];
[buyhereCell.contentView addSubview:despLabel];
UILabel *priceLabel = (UILabel *) [buyhereCell viewWithTag:1004];
priceLabel.text = [medicinePriceArray objectAtIndex:indexPath.row];
[buyhereCell.contentView addSubview:priceLabel];
UITextField *quantityTextField = (UITextField *) [buyhereCell viewWithTag:1005];
quantityTextField.delegate = self;
UIImageView *imgViewForDownArrow = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 22, 22)];
imgViewForDownArrow.image = [UIImage imageNamed:@"downArrow"];
[imgViewForDownArrow setContentMode:UIViewContentModeCenter];
quantityTextField.rightView = imgViewForDownArrow;
quantityTextField.rightViewMode = UITextFieldViewModeAlways;
UIPickerView *quantityPickerView= [[UIPickerView alloc]init];
quantityPickerView.delegate = self;
quantityPickerView.dataSource = self;
UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneBtnClicked:)];
doneBtn.tag = indexPath.row;
[toolBar setItems:[NSArray arrayWithObject:doneBtn] animated:NO];
quantityTextField.inputAccessoryView = toolBar;
quantityTextField.inputView = quantityPickerView;
[buyhereCell.contentView addSubview:quantityTextField];
UIButton *buyBtn = (UIButton *) [buyhereCell viewWithTag:1006];
buyBtn.tag = indexPath.row;
[buyBtn addTarget:self action:@selector(buyBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
[buyhereCell.contentView addSubview:buyBtn];
return buyhereCell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:NO];
NSLog(@"selected indexPath.row----%ld",indexPath.row);
}
-(void)doneBtnClicked:(UIBarButtonItem *)sender{
NSLog(@"sender.tag-----%ld",sender.tag);
NSIndexPath *indexPath=[NSIndexPath indexPathForItem:sender.tag inSection:0];
BuyhereTableViewCell *buyCell= [self.buyhereTableView cellForRowAtIndexPath:indexPath];
UITextField *quantityTextField = (UITextField *) [buyCell viewWithTag:1005];
NSLog(@"quantityTextField-----%@",quantityTextField);
quantityTextField.tag = indexPath.row;
quantityTextField.text = selectedQuantity;
[quantityTextField setDelegate: self];
[self.view endEditing:YES];
}
我得到了以下回复
2018-02-21 16:36:13.263524-0800 DoctorMyTPA[8043:262939] didSelectRow------3
2018-02-21 16:36:13.263775-0800 DoctorMyTPA[8043:262939] selectedQuantity------4
2018-02-21 16:36:15.069352-0800 DoctorMyTPA[8043:262939] didSelectRow------2
2018-02-21 16:36:15.069542-0800 DoctorMyTPA[8043:262939] selectedQuantity------3
2018-02-21 16:36:16.204510-0800 DoctorMyTPA[8043:262939] sender.tag-----0
2018-02-21 16:36:16.205001-0800 DoctorMyTPA[8043:262939] quantityTextField-----(null)