0
我在爲UIPickerview實現委託方法時面臨一個問題。問題的描述是,我想在pickerview中有3個組件,每個組件具有不同的行數。造成這種情況的代碼如下UIPickerView委託方法titleForRow執行不正確?
-(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次? 我使用一組數據填充行,所以數據源對於所有這些數據源都是通用的。這裏有什麼問題,我現在無法把握和困擾一段時間?任何指導是高度讚賞和提前感謝。
thnks @mvds,但你能詳細闡述一下嗎?糾正我,如果我錯了,但你的意思是,它的返回3值只會導致,只有3行可見? –
@experimentalist是的,當需要顯示更多的值時,它們被請求。只需旋轉選取器,您就會看到需要其他值。 – mvds
非常感謝解釋..它幫助我解決了我的問題!! :) –