我正在尝试为每列创建一个包含三列和不同行数的UIPickerView。选择器视图出现但每列只显示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];
}
答案 0 :(得分:0)
您的选择器每列只显示3个项目,因为这是- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
方法提供的内容。从该方法返回的数字是给定列(也称为组件)中将出现多少行。你在这里撤回_pickerData.count
,你的_pickerData
数组中有3个项目,所以这是有道理的。注意,数组的“count”计算数组中有多少个第一级对象 - 而不是每个对象中有多少个对象。
我想你要做的是返回_pickerData[component].count
而不是_pickerData.count
。这将获取选择器数据中的特定数组并返回该数组中的项目数,这看起来就像您正在寻找的那样。你已经在pickerView:TitleForRow:forComponent
做了类似的事情,所以我觉得你已经理解了这个概念。几乎就在那里。
要处理你的'id'警告,你可以强制转换对象告诉编译器它正在处理什么类型的对象:
NSArray *array = (NSArray*)_pickerData[component];
return array.count;
答案 1 :(得分:0)
问题在于pickerView:numberOfRowsInComponent:你在_pickerData中返回对象的数量。根据您的数据,该计数将始终为3 - 因为该数组包含3个数组。
您要做的是给出数组中与正在查看的组件相关的索引的项目数。所以你真正想要的是这样的:
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return ((NSArray*)_pickerData[component]).count;
}
希望有所帮助。
答案 2 :(得分:0)
您的代码应该在:
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return _pickerData objectAtIndex:component.count;
}