如何制作“无限”的UIPickerView?

时间:2012-02-09 07:46:58

标签: iphone objective-c ios ipad uipickerview

我有一个用户输入时间的选择器视图。当他们向下滚动到第23小时时,我希望小时0显示为下一个项目,并且要重新开始的序列。我怎样才能做到这一点?

这是我当前的选择器视图数据源:

- (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

2 个答案:

答案 0 :(得分:6)

我有一个自定义时间选择器(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;
}

答案 1 :(得分:1)

这是做你想做的事情的可能方法之一,对我来说并不美观,但是应该有效: Pauldy’s House of Geek Blog Archive The Abusive PickerView

您基本上是在创建批次,批次,批次重复的项目来模拟循环。