ios - 具有大量数值的 PickerView

标签 ios objective-c

我见过这样使用选择器 View 的示例,其中选择器的值被硬编码到源代码中

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    NSString * title = nil;
 switch(row) {
            case 0:
                title = @"a";
                break;
            case 1:
                title = @"b";
                break;
            case 2:
                title = @"c";
                break;
        }

但是,如果您要在选择器 View 中有 100 个数字,那将是非常不切实际的。我相信您可以在下面看到我正在尝试做的事情。它给我错误

expression is not an integer constant expression

如何在选择器 View 中获取从 0 到 100 的数字?如果有更好的方式让用户选择 1 到 100 之间的数字来输入,欢迎发表评论。

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

    if (pickerView.tag == 1) // this is otherPickerview
    {
        otherpickerview
        for (int i = 2; i < 100; i++){
            switch(row) {
                case i:
                    title = [NSString stringWithFormat:@"%d", i];
                    break;

            }


        }

    }

最佳答案

让我们试试:

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

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return _arr.count;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
// you can switch to other UIPickerView, and return title for row which you want.
// don't need to loop whenever titleForRow is called.
return _arr[row];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self.navigationController setNavigationBarHidden:YES];

_arr = [[NSMutableArray alloc]init];
for (int i = 0; i<100; i++) {
    NSString *str = [NSString stringWithFormat:@"%d",i];
    [_arr addObject:str];
}
_pickerView.dataSource = self;
_pickerView.delegate = self;
}

关于ios - 具有大量数值的 PickerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23190292/

相关文章:

ios - 只有在不同的 View Controller 上触发了某个 Action 后,我才能重新加载表格吗?

android - iOS 和 Android 设备 token 的长度是多少

iOS 以编程方式向联系人 ID 发送 WhatsApp 消息不再有效 (iOS 9)

objective-c - 如何将经典的 HFS 路径转换为 ​​POSIX 路径

ios - 调用具有关键字 "getter=isXXX"的 Objective-c 属性

iphone - 显式调用 loadView——好/坏?

c# - 如何从在 Xamarin IOS 中添加 UITabBarController 的另一个 Controller 更改标签的文本值

ios - 如何计算不同时区的两个 NSDate 对象之间的天数差异?

ios - isEqualToString 总是返回 False

iphone - 如何将图像存储在 iPhone 设备文件系统中,以便以后在我需要时可以检索它并在我的应用程序中使用?