Reputation: 2285
How can i change number Of Rows In each Components i have this:
- (NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return 3;
}
BUT this change all of Components rows number,
And The next question is : How can i Change the values in third component Like UIDatePicker : for example: in Date (in january) days are 31 but (in February) days are (29/28) an the third components must change and values will be between 1-29 or 1-28.
Upvotes: 1
Views: 2580
Reputation: 5416
You can change the number of rows for each component in the method
- (NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
switch(component)
{
case 0: // first component has 12 rows for example months
return 12;
case 1: // second component has 31 rows for example days
return 31;
}
}
And about your second question to calculate how many rows(days) will be in the 2nd component, you have to keep a variable to store which month is selected. Base on the selected month you can keep a variable to store how many rows(days) will be in the 2nd component. Let the variable is days which you have to update when the selection of 1st component(month) changed. Then, you have to just changed the previous codes as follows:
- (NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
switch(component)
{
case 0: // first component has 12 rows for example months
return 12;
case 1: // second component to show days
return days;
}
}
Hope you will understand...
Upvotes: 1
Reputation: 10009
Every picker calls -pickerView:numberOfRowsInComponent:
and asks it for the number of rows for each component. So this method is called three times, because you have three components. Thus you need to return different values for the components. You get an NSInteger in the component
variable to indicate which component is asked for:
- (NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
switch(component)
{
case 0: // first component has 42 rows
return 42;
case 1: // second component has 21 rows
return 21;
case 2: // third component has only two rows
return 2;
}
}
Upvotes: 8
Reputation: 1007
Depends on your need. Return different number.
- (NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if(CONDITION 1)
return 2;
else if(CONDITION 2)
return 3;
}
Upvotes: 1