根据其他组件重新加载pickerView的组件

时间:2013-01-09 17:27:56

标签: iphone ios objective-c xcode cocoa-touch

我有一个带有两个组件的pickerView,它们是“天”和“月”,我需要根据所选月份重新加载日期,例如:选择器视图从1月1日开始,1月有31天,所以我需要第一个组件有31个对象,日子......然后,用户将月份更改为4月,然后,我需要将第一个组件更改为30个对象...

我的代码是这样的:

- (void)viewDidLoad {

    [super viewDidLoad];

    list31 = [[NSMutableArray alloc] initWithObjects:@"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", @"10", @"11", @"12", @"13", @"14", @"15", @"16", @"17", @"18", @"19", @"20", @"21", @"22", @"23", @"24", @"25", @"26", @"27", @"28", @"29", @"30", @"31",nil];

    list30 = [[NSMutableArray alloc] initWithObjects:@"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", @"10", @"11", @"12", @"13", @"14", @"15", @"16", @"17", @"18", @"19", @"20", @"21", @"22", @"23", @"24", @"25", @"26", @"27", @"28", @"29", @"30",nil];

    list29 = [[NSMutableArray alloc] initWithObjects:@"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", @"10", @"11", @"12", @"13", @"14", @"15", @"16", @"17", @"18", @"19", @"20", @"21", @"22", @"23", @"24", @"25", @"26", @"27", @"28", @"29",nil];

    listMonth = [[NSMutableArray alloc] initWithObjects:@"Janeiro", @"Fevereiro", @"Março", @"Abril", @"Maio", @"Junho", @"Julho", @"Agosto", @"Setembro", @"Outubro", @"Novembro", @"Dezembro",  nil];

}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView{

    return 2;

}

-(NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{

    if(component == 0){
        if () {
            return [list31 count];
        } else if () {
            return [list30 count];
        } else if () {
            return [list29 count];
        }
    } else if(component == 1) {
        return [listMonth count];
    } 
}

-(NSString *) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{

    if(component == 0){
        if () {
            return [list31 objectAtIndex:row];
        } else if () {
            return [list30 objectAtIndex:row];
        } else if () {
            return [list29 objectAtIndex:row];
        }
    } else if(component == 1) {
        return [listMonth objectAtIndex:row];
    }
}

-(void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{

    if(component == 0){
       //dont know yet
    }
    if(component == 1){
        NSString *mes = [[NSString alloc] initWithFormat:@"%@", [listMonth objectAtIndex:row]];
        month.text = mes;
    }

}

我已准备好if但我不知道该放入表达区域...并且在“didSelectRow”上我对我将放在那里的代码一无所知。我用Google搜索了很多东西,但没找到任何东西......

*抱歉我的代码的某些部分是葡萄牙语,但它们只是变量名......

1 个答案:

答案 0 :(得分:0)

1.制作选择器视图的出口。
2.使用它来重新加载所有组件 - pickerView:didSelectRow:inComponent:
3.使用开关或标志来更改组件和数据源,因为一旦使用reloadAllcomponents将调用所有代理