Is it possible to make a selection list ( UIPickerView
) in a loop? Put the month of January right after the December, I say.
Is it possible to make a selection list ( UIPickerView
) in a loop? Put the month of January right after the December, I say.
Yes, but the method is kind of gambiarra, there is no automatic code for this so much that the apple itself does. I'm gonna explain; Smplismente make a list with 100 copies of the same sequence, then have that picker move up to sequence # 50 (item # 50 * 12 months).
Just like @Rivas
said, there is no very practical way to do it. I'll try to give you a better explanation of the above explanation:
You need to multiply the number of elements by a high number, 100, for example, but be careful, because depending on the object used, multiplying by a very high number can cause slowness.
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return self.mesesArray.count * 100;
}
Next, to get the title correctly you need to use the mod
( %
) operator to get the line corresponding to the 12 months.
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return self.mesesArray[row % self.mesesArray.count];
}
And to finalize, place the selection of UIPicker
in the middle of the list, or any next line. Do this in some method of initializing the view, such as viewDidLoad
.
- (void)viewDidLoad
{
[super viewDidLoad];
self.mesesArray = @[@"Janeiro",@"Fevereiro",@"Março",@"Abril",@"Maio",@"Junho",@"Julho",@"Agosto",@"Setembro",@"Outubro",@"Novembro",@"Dezembro"];
[self.pickerView selectRow:self.mesesArray.count * 50
inComponent:0
animated:NO];
}
The end result this: