尝试用3个列和不同的行列创建一个UIPickerView。

时间:2022-06-21 13:17:14

I'm trying to create a UIPickerView with three columns and different row count for each column. The picker view comes up but only shows 3 items per column. What I'm doing wrong? Thanks.

我尝试创建一个带有三个列的UIPickerView,每个列都有不同的行计数。picker视图出现了,但每行只显示3个条目。我做错了什么吗?谢谢。

_pickerData = @[ @[@"blanc", @"1/8", @"1/4", @"1/2", @"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", @"10"],
                         @[@"blanc", @"1/8", @"1/4", @"1/2", @"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9", @"10"],
                         @[@"item 2", @"item 3", @"item 4", @"item 5", @"item 6", @"item 7", @"item 8", @"item 9", @"item 10"]];


// The number of columns of data
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 3;
}

// The number of rows of data
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return _pickerData.count;
}

// The data to return for the row and component (column) that's being passed in
- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return _pickerData[component][row];
}

3 个解决方案

#1


0  

Your picker is only showing 3 items per column because that's what's provided by the - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component method. Whatever number returned from this method is how many rows will appear in a given column (aka component). You are returing _pickerData.count here, and there are 3 items in your _pickerData array, so this makes sense. Note, the "count" of an array counts how many first level objects there are in the array - not how many objects are within each of those objects.

您的选择器只显示每列3个项,因为这是由- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)组件方法提供的。无论从这个方法返回的数字是多少行将出现在给定的列中(即组件)。你是杀敌_pickerData。在这里计算,在您的_pickerData数组中有3个条目,所以这是有意义的。注意,数组的“count”计数在数组中有多少个一级对象,而不是每个对象中有多少个对象。

What I imagine you want to do is return _pickerData[component].count instead of _pickerData.count. This will grab a specific array within your picker data and return the number of items within that array, which seems like it's what you are looking for. You're already doing something similar in pickerView:TitleForRow:forComponent so I think you understand the concept already. Almost there.

我想要做的是返回_pickerData[组件]。而不是_pickerData.count计数。这将在您的picker数据中获取一个特定的数组,并返回该数组中的项数,这看起来是您正在寻找的。您已经在pickerView:TitleForRow中做了类似的事情:forComponent,所以我认为您已经理解了这个概念。差不多了。

To deal with your 'id' warning, you can cast the object to tell the compiler what kind of object it is dealing with:

为了处理您的“id”警告,您可以将对象转换为告诉编译器它处理的对象类型:

NSArray *array = (NSArray*)_pickerData[component];
return array.count;

#2


0  

The problem is in pickerView:numberOfRowsInComponent: where you are returning the count of objects in _pickerData. That count, based on your data, will always be 3 - because the array contains 3 arrays.

问题是在pickerView:numberOfRowsInComponent:您在哪里返回_pickerData中的对象计数。这个计数,根据你的数据,将永远是3 -因为数组包含3个数组。

What you want to do is give the count of items within the array for the index related to the component being looked at. So what you really want is something like this:

您要做的是在数组中给出与被查看的组件相关的索引项的计数。所以你真正想要的是这样的东西:

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return ((NSArray*)_pickerData[component]).count;
}

Hope that helps.

希望有帮助。

#3


0  

your code should be in:​​

你的代码应该在:

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

#1


0  

Your picker is only showing 3 items per column because that's what's provided by the - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component method. Whatever number returned from this method is how many rows will appear in a given column (aka component). You are returing _pickerData.count here, and there are 3 items in your _pickerData array, so this makes sense. Note, the "count" of an array counts how many first level objects there are in the array - not how many objects are within each of those objects.

您的选择器只显示每列3个项,因为这是由- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)组件方法提供的。无论从这个方法返回的数字是多少行将出现在给定的列中(即组件)。你是杀敌_pickerData。在这里计算,在您的_pickerData数组中有3个条目,所以这是有意义的。注意,数组的“count”计数在数组中有多少个一级对象,而不是每个对象中有多少个对象。

What I imagine you want to do is return _pickerData[component].count instead of _pickerData.count. This will grab a specific array within your picker data and return the number of items within that array, which seems like it's what you are looking for. You're already doing something similar in pickerView:TitleForRow:forComponent so I think you understand the concept already. Almost there.

我想要做的是返回_pickerData[组件]。而不是_pickerData.count计数。这将在您的picker数据中获取一个特定的数组,并返回该数组中的项数,这看起来是您正在寻找的。您已经在pickerView:TitleForRow中做了类似的事情:forComponent,所以我认为您已经理解了这个概念。差不多了。

To deal with your 'id' warning, you can cast the object to tell the compiler what kind of object it is dealing with:

为了处理您的“id”警告,您可以将对象转换为告诉编译器它处理的对象类型:

NSArray *array = (NSArray*)_pickerData[component];
return array.count;

#2


0  

The problem is in pickerView:numberOfRowsInComponent: where you are returning the count of objects in _pickerData. That count, based on your data, will always be 3 - because the array contains 3 arrays.

问题是在pickerView:numberOfRowsInComponent:您在哪里返回_pickerData中的对象计数。这个计数,根据你的数据,将永远是3 -因为数组包含3个数组。

What you want to do is give the count of items within the array for the index related to the component being looked at. So what you really want is something like this:

您要做的是在数组中给出与被查看的组件相关的索引项的计数。所以你真正想要的是这样的东西:

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return ((NSArray*)_pickerData[component]).count;
}

Hope that helps.

希望有帮助。

#3


0  

your code should be in:​​

你的代码应该在:

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