2012-06-29 137 views
0

我在一个视图中有4个UIpickerviews。我想用一个数组填充它们。目前,当视图出现时,它会显示一个包含数据的选择器视图,但其他视图是空的。当我开始滚动第二个时,它开始填充,但第一个开始变空。我使用通常的方式来填充UIPickerVIew。用一个数组填充多个UIPickerview?

+0

显示一些代码,列,什么样的数据你有你的阵列里,而且你打算如何在单组分成4套等等...? – trumpetlicks

回答

0

作为对您的问题的答案的开始,您将不得不实施UIPickerViewDataSource的委托例程。

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ 
    return 4; 
} 

你可能已经在你的代码上面的例程!

接下来,您将必须实现以下几点:

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{ 
    switch(component){ 
     case 0: 
      // return X based upon how many for the left-most column 
     case 1: 
      // return X based upon how many for the second left-most column 
     case 2: 
      // return X based upon how many for the third left-most column 
     default: // Serves as 4 
      // return X based upon how many for the right-most column   
    } 
} 

还是在根据上述程序就可以简单(如果你的数据来填充为n的每一列这样你的数组的大小应该是n * 4)是

[myArray count]; // Where myArray is replaced with you array variable name 

最后实现:

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

    // Assuming your array is filled with strings only 
    int index = [myArray count]/4 * component + row; 
    return [myArray objectAtIndex:index]; 
} 

这是假设你的数组看起来像

[1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4] 

,其中数字表示该数据属于,而不是数据本身