如何制作“无限” UIPickerView?
我有一个选择器视图,用户在其中输入时间。当他们向下滚动到 23 小时时,我希望零时显示为下一个项目,并且序列重新开始。我怎样才能实现这个目标?
这是我当前的选择器视图数据源:
- (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;
}
I have a picker view where the user is entering a time. When they scroll down to hour 23, I want hour zero to appear as the next item, and the sequence to re-start. How can I achieve this?
Here is my current picker view data source:
- (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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我有一个自定义时间选择器(UIPickerView),其委托如下所示:
I have a custom time picker (UIPickerView), for which the delegate looks like this:
这是做你想做的事情的可能方法之一,对我来说看起来不太漂亮,但应该有效:
Pauldy's House of Geek 博客档案 The Abusive PickerView
你基本上是创建很多很多重复的项目来模拟循环。
Here's one of the possible ways to do what you want, doesn't look pretty for me, but should work:
Pauldy’s House of Geek Blog Archive The Abusive PickerView
You are basically creating lot, lot, lot duplicate items to simulate a loop.