伙计们,我在为 UIPickerview 实现委托方法时遇到了一个问题。问题的描述是,我想在pickerview中有3个组件,每个组件都有不同的行数。代码如下
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 3;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
NSInteger rows;
if (0 == component)
rows = 5;
else if (1 == component)
rows = 10;
else
rows = 15;
NSLog(@"Number of rows returned for component %d are rows %d",component, rows);
return rows;
}
在此之后,我正在实现“titleForRow”委托方法代码,如下所示:
#pragma -mark UIPIckerView Delegate Methods
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
//NSString *temp;
//temp = [[NSString alloc] initWithString:[self.pocModelData.arrayOfDistricts objectAtIndex:row]];
if (0 == component)
{
NSLog(@"String returned is %@ for row %d for component %d", [self.pocModelData.arrayOfDistricts objectAtIndex:row], row, component);
return [self.pocModelData.arrayOfDistricts objectAtIndex:row];
}
else if
(1 == component){
NSLog(@"String returned is %@ for row %d for component %d", [self.pocModelData.arrayOfDistricts objectAtIndex:row], row, component);
return [self.pocModelData.arrayOfDistricts objectAtIndex:row];
}else{
NSLog(@"String returned is %@ for row %d for component %d", [self.pocModelData.arrayOfDistricts objectAtIndex:row], row, component);
return [self.pocModelData.arrayOfDistricts objectAtIndex:row];
}
//return temp;
// return [self.pocModelData.arrayOfDistricts objectAtIndex:row];
}
此方法在执行时只为每个组件返回 3 行,如以下语句所示
2013-05-18 02:00:40.743 PopOverControllerExample[8228:c07] Number of rows returned for component 0 are rows 5
2013-05-18 02:00:41.221 PopOverControllerExample[8228:c07] Number of rows returned for component 1 are rows 10
2013-05-18 02:00:41.696 PopOverControllerExample[8228:c07] Number of rows returned for component 2 are rows 15
2013-05-18 02:00:42.035 PopOverControllerExample[8228:c07] Number of rows returned for component 0 are rows 5
2013-05-18 02:00:43.761 PopOverControllerExample[8228:c07] String returned is 1 for row 0 for component 0
2013-05-18 02:00:44.687 PopOverControllerExample[8228:c07] String returned is 2 for row 1 for component 0
2013-05-18 02:00:45.048 PopOverControllerExample[8228:c07] String returned is 3 for row 2 for component 0
2013-05-18 02:00:45.544 PopOverControllerExample[8228:c07] Number of rows returned for component 1 are rows 10
2013-05-18 02:00:46.131 PopOverControllerExample[8228:c07] String returned is 1 for row 0 for component 1
2013-05-18 02:00:46.620 PopOverControllerExample[8228:c07] String returned is 2 for row 1 for component 1
2013-05-18 02:00:47.380 PopOverControllerExample[8228:c07] String returned is 3 for row 2 for component 1
2013-05-18 02:00:47.772 PopOverControllerExample[8228:c07] Number of rows returned for component 2 are rows 15
2013-05-18 02:00:48.543 PopOverControllerExample[8228:c07] String returned is 1 for row 0 for component 2
2013-05-18 02:00:48.915 PopOverControllerExample[8228:c07] String returned is 2 for row 1 for component 2
2013-05-18 02:00:49.461 PopOverControllerExample[8228:c07] String returned is 3 for row 2 for component 2
我无法理解为什么每个组件只执行 3 次?我正在用一组数据填充行,因此数据源对所有行都是通用的。这里有什么问题,我现在无法掌握并且卡住了一段时间?任何指导都非常感谢,并在此先感谢。