xcode 如何以编程方式将 1-100 个数字添加到选择器视图?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7875946/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:13:20  来源:igfitidea点击:

How can I add 1-100 numbers to picker view programmatically?

iphoneobjective-cxcode

提问by venkateswararao burle

I created a UIPickerView manually with three components. I want to display numbers from 1 to 100 in component1. How can I do it programmatically?

我用三个组件手动创建了一个 UIPickerView。我想在 component1 中显示从 1 到 100 的数字。我怎样才能以编程方式做到这一点?

回答by Ilanchezhian

I guess you have implemented the methods

我猜你已经实现了这些方法

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

and

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

    if (component == 1 )
    {
        return [NSString stringWithFormat:@"%d",(row+1)];
    }

    return @"";//required elements
}

回答by aahsanali

in .h file

在 .h 文件中

NSMutableArray *data ;

--

——

data = [NSMutableArray alloc] init];
for (int i=1; i<=100; i++){
     [data addObject:[NSString stringWithFormat:@"%d",i];
}

// this will add 1-100 values in your array (data) Then Implement UIPickerViewDelegate

// 这将在您的数组(数据)中添加 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];
}

回答by Narayana

use below code it wil help you

使用下面的代码它会帮助你

-(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];

}

回答by Jonatan

Here's a quick answer (just passing through). Use an array for storing the values. Use some kind of loop to quickly add the values 1-1000. Use something like this to get the value:

这是一个快速答案(只是通过)。使用数组来存储值。使用某种循环快速添加值 1-1000。使用这样的东西来获取值:

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:   (NSInteger)component{
 return [component1Array objectAtIndex: row];
}