开发者

How can i change number Of Rows In each Components

How can i change number Of Rows In each Components

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 thir开发者_高级运维d components must change and values will be between 1-29 or 1-28.


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;
    }
}


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;
}


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...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜