我正在使用3列的选择器视图。我想确定第二列的第3列值。这是我的代码的一部分
- (void)viewDidLoad {
[super viewDidLoad];
Number=@[@"Trans",@"1st",@"2nd",@"3rd",@"4th",@"5th",@"6th",@"7th",@"8th",@"9th",@"10th",@"11th",@"12th"];
Season=@[@"Spring",@"Summer",@"Fall"];
Course=@[@"CHEM1100 General Chem I",@"CHEM2100 General Chem II",@"CHEM3511 Org Chem",@"CHEM3521 Org Chem II"];
// Course=@[@"Summer1",@"Summer2",@"Summer3"];
Course = [Course sortedArrayUsingSelector:@selector(compare:)];
Number =[Number sortedArrayUsingSelector:@selector(compare:)];
Season =[Season sortedArrayUsingSelector:@selector(compare:)];
}
这是我的问题,我怎样才能实现像
这样的目标if (Season==@"Spring")
Course= @[@"CHEM1100 General Chem I",@"CHEM2100 General Chem II",@"CHEM3511 Org Chem",@"CHEM3521 Org Chem II"];
else if (Season==@"Summer")
Course=@[@"Summer1",@"Summer2",@"Summer3"];
抱歉,我不知道用什么方法来完成代码。好吗?
要显示列值,请使用以下代码:
- (IBAction)addCourse:(UIButton *)sender {
NSInteger numRow=[picker selectedRowInComponent:kNumComponent];//0=1st,1=2nd,etc
NSInteger SeaRow=[picker selectedRowInComponent:kSeaComponent];//0=fall,1=spring,2=summer
NSInteger CourseRow=[picker selectedRowInComponent:kCourseComponent];
NSString *num=Number[numRow];
NSString *season=Season[SeaRow];
NSString *course=Course[CourseRow];
NSString *msg=[[NSString alloc ]initWithFormat:@"%@ ",course];
_courseLabel.text=[msg substringToIndex:8];
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
if (component==kNumComponent)
return [Number count];
else if(component==kSeaComponent)
return [Season count];
else return [Course count];
}
答案 0 :(得分:1)
根据文档,您必须为UIPickerView提供数据源和委托。数据源告诉视图有多少组件和行,而委托告诉它组件行的内容。让我们假设您在委托中实现titleForRow:由于这将根据选择进行更改,因此您需要做两件事:(i)确保您的委托对象知道做出了哪些选择(例如@“Spring”)和(ii)您将需要调用UIPickerView reloadComponent:方法,以便调用你的委托的titleForRow:方法。
示例:假设您的操作方法是componentSelected:
-(void)componentSelected:(id)sender
{
NSInteger row = [myPicker selectedRowInComponent:seasonComponent];
myDelegate.season = row;
[myPicker reloadComponent:courseComponent];
}
顺便说一下,养成习惯是要非常小心“=”,“==”,isEqualToString:等以避免错误。您的问题有几个基本的语法错误。这条线
if(season = @"spring")
应该是
if([season isEqualToString:@"spring"]);
不仅因为“=”是赋值,而且因为“==”没有意义,如果其中一个参数是文字(它本质上是指针比较)。 isEqualToString将比较目标字符串和参数字符串。