如何制作“无限” UIPickerView?

发布于 2025-01-03 21:40:47 字数 2585 浏览 2 评论 0原文

我有一个选择器视图,用户在其中输入时间。当他们向下滚动到 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; 
}

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

野心澎湃 2025-01-10 21:40:47

我有一个自定义时间选择器(UIPickerView),其委托如下所示:

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 4; // it's with am/pm in my case
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    /// 18000 is divisible with 12, 24 (for the hour) and 60 (for the minutes and seconds)
    /// it can still get to the end.. but will need a lot of scrolling
    /// I set the initial value between 9000 and 9060
    if (component==0) { // hour
        return 18000;
    }
    if (component==3) { // am/pm
        return [_datasource count];
    }
      // seconds and minutes - could use 45000 so it repeats the same number of time as the hours
    return 18000;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if (component==3) { // am/pm
        return [_datasource objectAtIndex:row];
    }
    if (component==0) { // hour
        return [NSString stringWithFormat:@"%d", row%12];
    }
    return [NSString stringWithFormat:@"%02d", row%60];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    if (component==3) { // am/pm
        if (row==0&&_h>11) {
            _h-=12;
        }
        if (row==1&_h<12) {
            _h+=12;
        }
    }
    else if(component==0){
        _h=row%12;
        if ([self selectedRowInComponent:3]==1) { // handle am/pm
            _h+=12;
        }
    }
    else if(component==1){
        _m=row%60;
    }
    else if(component==2){
        _s=row%60;
    }
}
-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
    // set the size for components
    if (component<3) {
        return 50;
    }
    return 70;
}

I have a custom time picker (UIPickerView), for which the delegate looks like this:

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 4; // it's with am/pm in my case
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    /// 18000 is divisible with 12, 24 (for the hour) and 60 (for the minutes and seconds)
    /// it can still get to the end.. but will need a lot of scrolling
    /// I set the initial value between 9000 and 9060
    if (component==0) { // hour
        return 18000;
    }
    if (component==3) { // am/pm
        return [_datasource count];
    }
      // seconds and minutes - could use 45000 so it repeats the same number of time as the hours
    return 18000;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if (component==3) { // am/pm
        return [_datasource objectAtIndex:row];
    }
    if (component==0) { // hour
        return [NSString stringWithFormat:@"%d", row%12];
    }
    return [NSString stringWithFormat:@"%02d", row%60];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    if (component==3) { // am/pm
        if (row==0&&_h>11) {
            _h-=12;
        }
        if (row==1&_h<12) {
            _h+=12;
        }
    }
    else if(component==0){
        _h=row%12;
        if ([self selectedRowInComponent:3]==1) { // handle am/pm
            _h+=12;
        }
    }
    else if(component==1){
        _m=row%60;
    }
    else if(component==2){
        _s=row%60;
    }
}
-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
    // set the size for components
    if (component<3) {
        return 50;
    }
    return 70;
}
葬心 2025-01-10 21:40:47

这是做你想做的事情的可能方法之一,对我来说看起来不太漂亮,但应该有效:
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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文