Objective C如何根据其他数组项从数组中删除项
Posted
技术标签:
【中文标题】Objective C如何根据其他数组项从数组中删除项【英文标题】:Objective C how to remove items from array based on other arrays items 【发布时间】:2020-04-21 09:43:52 【问题描述】:我有三个 UIPickerviews。我有一个json响应。我有一个三个文本字段。如果用户选择第一个选择器视图,则相关数据将显示在第一个文本字段中。第二个和第三个选择器视图也相同。我的要求是如果用户在第一个选择器视图中选择特定项目,第二个和第三个选择器视图不显示该项目。第二和第三也一样。
-(void)viewDidLoad
dataArray = [[NSMutableArray alloc]initWithObjects:@"A+",@"A-",@"B+",@"B-",@"O+",@"O-", nil];
bloodGroup = [[UITextField alloc]initWithFrame:CGRectMake(10, logoImg.frame.origin.y+logoImg.frame.size.height+45, screenWidth-20, 50)];
bloodGroup.borderStyle = UITextBorderStyleRoundedRect;
bloodGroup.font = [UIFont systemFontOfSize:15];
bloodGroup.placeholder = @"Please Select Your Option";
bloodGroup.delegate = self;
[self.view addSubview:bloodGroup];
txtField1 = [[UITextField alloc]initWithFrame:CGRectMake(10, ansField1.frame.origin.y+ansField1.frame.size.height+45, screenWidth-20, 50)];
txtField1.borderStyle = UITextBorderStyleRoundedRect;
txtField1.font = [UIFont systemFontOfSize:15];
txtField1.placeholder = @"Please Select Your Option";
txtField1.delegate = self;
[self.view addSubview:txtField1];
txtField2 = [[UITextField alloc]initWithFrame:CGRectMake(10, ansField2.frame.origin.y+ansField2.frame.size.height+45, screenWidth-20, 50)];
txtField2.borderStyle = UITextBorderStyleRoundedRect;
txtField2.font = [UIFont systemFontOfSize:15];
txtField2.placeholder = @"Please Select Your Option";
txtField2.delegate = self;
[self.view addSubview:txtField2];
myPickerView = [[UIPickerView alloc] init];
[myPickerView setDataSource: self];
[myPickerView setDelegate: self];
myPickerView.showsSelectionIndicator = YES;
bloodGroup.inputView = myPickerView;
bloodGroup.inputAccessoryView = toolBar;
// txtField1
txtField1.inputView = myPickerView;
txtField1.inputAccessoryView = toolBar;
txtField2.inputView = myPickerView;
txtField2.inputAccessoryView = toolBar;
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
if (isBloodGroupFieldSelected)
return 1;
else if(!isBloodGroupFieldSelected)
return 1;
else if(!isGenderGroupFieldSelected)
return 1;
return 0;
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
if (isBloodGroupFieldSelected)
return [dataArray count];
else if(!isBloodGroupFieldSelected)
return [dataArray count];
else if (!isGenderGroupFieldSelected)
return [dataArray count];
return 0;
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
if (isBloodGroupFieldSelected)
return dataArray[row];
else if((!isBloodGroupFieldSelected) && (isGenderGroupFieldSelected))
return dataArray[row];
else if(!isGenderGroupFieldSelected)
return dataArray[row];
return 0;
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
if (isBloodGroupFieldSelected)
bloodGroup.text = dataArray[row];
else if((!isBloodGroupFieldSelected) && (isGenderGroupFieldSelected))
txtField1.text = dataArray[row];
else if(!isGenderGroupFieldSelected)
txtField2.text= dataArray[row];
- (void)textFieldDidBeginEditing:(UITextField *)textField
if (textField == bloodGroup)
isBloodGroupFieldSelected = YES;
else if (textField == txtField1)
isBloodGroupFieldSelected = NO;
isGenderGroupFieldSelected = YES;
else if (textField == txtField2)
isGenderGroupFieldSelected = NO;
isBloodGroupFieldSelected = NO;
[myPickerView reloadAllComponents];
【问题讨论】:
【参考方案1】:您已经拥有了大部分所需的代码。 pickerView:didSelectRow:inComponent:
委托方法将在您的任何选择器已更改时被调用,假设它们每个都将其委托设置为上面的控制器。虽然,我只看到上面代码中定义的一个选择器视图。假设您的选择器视图名为myPickerView
、myPickerView2
和myPickerView3
。代码大概是:
- (void)pickerView:(UIPickerView *)pickerView
didSelectRow:(NSInteger)row
inComponent:(NSInteger)component
if (pickerView == pickerView1)
// the first picker view lseection has changed
// update myPickerView2 and myPickerView3 to reflect
// the values you want based on this selection
else if (pickerView == pickerView2)
// update the other views based on this selection
else ...
....
【讨论】:
感谢您的回复。但我正在使用单个选择器视图。请解释完整的代码。以上是关于Objective C如何根据其他数组项从数组中删除项的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Objective C 中形成 JSON 格式的可变数组