我用三个组件手动创建了一个UIPickerView。我想在component1中显示从1到100的数字。我该如何以编程方式完成?
答案 0 :(得分:8)
我猜你已经实现了方法
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 3;
}
和
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
if (component == 1 )
{
return [NSString stringWithFormat:@"%d",(row+1)];
}
return @"";//required elements
}
答案 1 :(得分:4)
NSMutableArray *data ;
-
data = [NSMutableArray alloc] init];
for (int i=1; i<=100; i++){
[data addObject:[NSString stringWithFormat:@"%d",i];
}
//这将在您的数组中添加1-100个值(数据) 然后实现UIPickerViewDelegate
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
return [data count];
}
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [data objectAtIndex:row];
}
答案 2 :(得分:1)
使用以下代码可以帮助您
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if(component==1)
return 100;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if(component==1)
return [NSString stringWithFormat:@"%i",row+1];
}
答案 3 :(得分:0)
这是一个快速回答(只是通过)。使用数组存储值。使用某种循环快速添加值1-1000。使用类似的东西来获得价值:
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent: (NSInteger)component{
return [component1Array objectAtIndex: row];
}