iOS – 填充静态UIPickerView

我有一个配置文件表单作为iOS应用程序注册过程的一部分.

我想使用一个“下拉”菜单,如性别,标题,dob等项目.每个数据将是静态的 – 我将使用UIPickerView实现 – 但我的问题是 – 我需要创建数组和数据委托来填充每个单独的选择器,还是有一个更简单的方法来应用静态数据?

解决方法

没有代表你可以做吗?没有.

你可以没有数组吗?是的,但你不应该.

这是一个没有数组的例子(从http://cocoamatic.blogspot.com/2010/08/create-uipickerview-programmatically.html).

- (void)pickerView:(UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent:(NSInteger)component {
    // Handle the selection
}

// tell the picker how many rows are available for a given component
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    NSUInteger numRows = 3;

    return numRows;
}

// tell the picker how many components it will have
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
 return 1;
}

// tell the picker the title for a given component
- (Nsstring *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    Nsstring *title;

    title = [@"" stringByAppendingFormat:@"Row %d",row];

    return title;
}

// tell the picker the width of each row for a given component
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
 int sectionWidth = 300;

 return sectionWidth;
}

然而,你真的应该使用一个数组,使它更容易阅读维护.如果要添加额外的值,只需将其添加到数组中即可.而不是在多个位置进行更新,只需在数组中添加一个值即可.此外,更容易理解.

@implementation PickerViewController
.
.
- (void)viewDidLoad {
      [super viewDidLoad];
      _myArray = @[@"Row 1",@"Row 2",@"Row 3",];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent:(NSInteger)component {
    // Handle the selection
}

// tell the picker how many rows are available for a given component
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    NSUInteger numRows = 3;

    return _myArray.count;
}

// tell the picker how many components it will have
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
 return 1;
}

// tell the picker the title for a given component
- (Nsstring *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    Nsstring *title;

    title = myArray[row];

    return title;
}

// tell the picker the width of each row for a given component
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
 int sectionWidth = 300;

 return sectionWidth;
}

如果您有多个选择器,则只需使用多个数组,并检查每个PickerView,以查看从PickerView传递到列出的每个函数之后的哪一个.

@implementation PickerViewController
.
.
- (void)viewDidLoad {
      [super viewDidLoad];
      _myArray1 = @[@"Row 1",];
      _myArray2 = @[@"Row 1-2",@"Row 2-2",@"Row 3-2",@"Row 4-2"];      

      UIPickerView pickerView1 = [[UIPickerView alloc] init];
      UIPickerView pickerView2 = [[UIPickerView alloc] init];      
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent:(NSInteger)component {
    // Handle the selection
    if (pickerView == pickerView1)
    {
        // First Picker
    }
    else if (pickerView == pickerView2)
    {
        // First Picker    
    }
}

// tell the picker how many rows are available for a given component
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (pickerView == pickerView1)
    {
        // First Picker
         return _myArray1.count;
    }
    else if (pickerView == pickerView2)
    {
        // Second Picker    
        return _myArray2.count;
    }    

    // A third picker passed in somehow
    return 0;
}

// tell the picker how many components it will have
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    if (pickerView == pickerView1)
    {
        // First Picker
         return 1;
    }
    else if (pickerView == pickerView2)
    {
        // Second Picker    
        return 1;
    }    

    // A third picker passed in somehow
    return 0;
}

// tell the picker the title for a given component
- (Nsstring *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    Nsstring *title;

    if (pickerView == pickerView1)
    {
        // First Picker
         title = myArray1[row];
    }
    else if (pickerView == pickerView2)
    {
        // Second Picker    
        rtitle = myArray2[row];
    }    

    return title;
}

// tell the picker the width of each row for a given component
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    if (pickerView == pickerView1)
    {
        // First Picker
         return 300;
    }
    else if (pickerView == pickerView2)
    {
        // Second Picker    
        return 400;
    }    

    // A third picker passed in somehow
    return 0;
}

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...