gpt4 book ai didi

ios - 具有大量数值的 PickerView

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:23:47 26 4
gpt4 key购买 nike

我见过这样使用选择器 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com