Dan*_*cer 10 objective-c uipickerview ios
我有一个个人资料表格作为iOS应用程序注册过程的一部分.
我想对性别,标题,DOB等项目使用"下拉"菜单.每个项目的数据都是静态的 - 我将使用UIPickerView来实现 - 但我的问题是 - 我是否需要创建数组和数据委托填充每个单独的选择器还是有更简单的方法来应用静态数据?
ans*_*ble 14
你能在没有代表的情况下做到吗?没有.
没有阵列你能做到吗?是的,但你不应该.
这是一个没有数组的例子(来自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;
}
Run Code Online (Sandbox Code Playgroud)
但是,你真的应该只使用一个数组,这样可以更容易地阅读维护.如果要添加其他值,只需将其添加到阵列即可.只需在数组中添加另一个值,而不是在多个位置进行更新.此外,更容易理解.
@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;
}
Run Code Online (Sandbox Code Playgroud)
如果您有多个选择器,您只需使用多个数组并检查每个PickerView,看看自从PickerView传递到列出的每个函数后您拥有了哪个.
@implementation PickerViewController
.
.
- (void)viewDidLoad {
[super viewDidLoad];
_myArray1 = @[@"Row 1", @"Row 2", @"Row 3",];
_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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7471 次 |
| 最近记录: |