我有一個選擇器視圖,用戶正在輸入一個時間。當他們向下滾動到23小時時,我想要小時零作爲下一個項目出現,並且要重新開始序列。我怎樣才能做到這一點?如何製作「無限」的UIPickerView?
這是我目前的選擇器視圖的數據源:
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 3;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
NSArray *sectionContents = [[self dataForDatapickerRows] objectAtIndex:component];
NSInteger rows = [sectionContents count];
NSLog(@"component %d rows is: %d",component,rows);
return rows;
}
- (NSString *)pickerView:(UIPickerView *)pickerView
titleForRow:(NSInteger)row
forComponent:(NSInteger)component {
self.dataPickerInstance.backgroundColor=[UIColor whiteColor];
return ([[[self dataForDatapickerRows ]objectAtIndex:component] objectAtIndex:row]);
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if(component==0)
{
[self setHou:(NSInteger*)[[[self dataForDatapickerRows ]objectAtIndex:component] objectAtIndex:row]];
}
else if(component==1)
{
[self setMinut:(NSInteger*)[[[self dataForDatapickerRows ]objectAtIndex:component] objectAtIndex:row]];
}
else if(component==2)
{
[self setSecon:(NSInteger*)[[[self dataForDatapickerRows ]objectAtIndex:component] objectAtIndex:row]];
}
NSLog(@"%@",[[[self dataForDatapickerRows ]objectAtIndex:component] objectAtIndex:row]);
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
UILabel* tView = (UILabel*)view;
if (!tView){
tView = [[UILabel alloc] init];
[tView setFrame:CGRectMake(0, 0, 90,60)];
tView.self.textColor=[UIColor whiteColor];
tView.minimumFontSize = 80;
tView.textAlignment=UITextAlignmentCenter;
tView.adjustsFontSizeToFitWidth = YES;
tView.autoresizesSubviews=YES;
tView.font = [UIFont boldSystemFontOfSize:40];
tView.backgroundColor = [UIColor blackColor];
//tView.highlightedTextColor=[UIColor greenColor];
// factLabel.frame = CGRectMake(factLabel.frame.origin.x, factLabel.frame.origin.y, factLabel.frame.size.width, lLabelSIze.height);
//
// Setup label properties - frame, font, colors etc
} // Fill the label text here
tView.text=[[[self dataForDatapickerRows ]objectAtIndex:component] objectAtIndex:row];
return tView;
}
喜的朋友,首先我要表示感謝,以you.In你的代碼,我需要增加行數到18000,但我只需要24小時和60秒的第二和分鐘,然後我需要做的。你有什麼想法嗎?請分享 – Spynet 2012-02-09 08:23:04
首先,從所有方法中刪除最後一個組件(am/pm)。然後,你需要爲你的需要修改'pickerView:titleForRow:forComponent:'(例如:在0-23之間使用'[NSString stringWithFormat:@「%d」,row%24]',要使用1到24之間的值,請使用'[NSString stringWithFormat:@「%d」,row%24 + 1]')。秒和分鐘的值只能在0到59之間。請檢查user792677後的鏈接,它可能會更好地解釋(它使用相同的想法,並在選擇新值後滾動到中間)。 – 2012-02-09 08:37:54
oh ..你還需要修改'pickerView:didSelectRow:inComponent:'返回相同的值(刪除第一個if - am/pm - at'component == 0''_h'(hour)將是row %24(或行%24 + 1,取決於你如何在'titleForRow:'方法中使用它) – 2012-02-09 08:47:24