Comment Obtenir La Valeur Sélectionnée De UIPickerView Avec 2 Composants

J'ai une question ici, où j'ai demandé comment faire pour obtenir une valeur de la forme UIPickerView ( Comment Obtenir La Valeur Sélectionnée De UIPickerView ). J'ai une solution pour un sélecteur avec 1 composant, mais je vais essayer d'obtenir la valeur de sélecteur avec 2 composant.

Ce code fonctionne pour sélecteur à 1 composant:

    NSInteger row;
    NSString *weightSelected;

    row = [repPicker selectedRowInComponent:0];
    weightSelected = [pickerArray objectAtIndex:row];

Je fatigué ce code pour mon sélecteur avec 2 composants, mais c'est la congélation. Il n'y a pas d'erreur dans la console. La ligne, c'est le point de congélation est row2 = [repPicker selectedRowInComponent:1];
:

    NSInteger row1, row2;
    NSString *weightSelected1;
    NSString *weightSelected2;

    row1 = [repPicker selectedRowInComponent:0];
    row2 = [repPicker selectedRowInComponent:1];
    weightSelected1 = [pickerArray objectAtIndex:row1];
    weightSelected2 = [pickerArray objectAtIndex:row2];
    NSString *weightSelected = [NSString stringWithFormat:@"%@.%@", weightSelected1, weightSelected2];

Mise à JOUR:

#pragma mark - Weight Picker 
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView 
{
if (thePickerView.tag==1)
{
return 2;
}
else
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component 
{
if (thePickerView.tag==1)
{
if (component == 0)
return [pickerArray count];
if (component == 1)
return [pickerArrayHalf count];
}
else
return [pickerArray count];
}
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component 
{
//NSLog(@"Selected Color: %@. Index of selected color: %i", [weightArray objectAtIndex:row], row);
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
if (pickerView.tag==1)
{
if (component == 0)
{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 37)];
int weight = [[pickerArray objectAtIndex:row] intValue];
label.text = [NSString stringWithFormat:@"%d", weight];
label.textAlignment = UITextAlignmentCenter;
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:30];
[label autorelease];
return label;
}
else
{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 37)];
label.text = [pickerArrayHalf objectAtIndex:row];
label.textAlignment = UITextAlignmentCenter;
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:30];
[label autorelease];
return label;
}
}
else
{
int reps = [[pickerArray objectAtIndex:row] intValue];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 37)];
label.text = [NSString stringWithFormat:@"%d reps", reps];
label.textAlignment = UITextAlignmentCenter;
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:30];
[label autorelease];
return label;
}
}

Mise à jour:

J'ai ajouté ce code:

- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component 
{
NSLog(@"Selected Color: %@. Index of selected row: %i", [pickerArray objectAtIndex:row], row);
NSLog(@"Component 0: %@", [repPicker selectedRowInComponent:0]);
NSLog(@"Component 1: %@", [repPicker selectedRowInComponent:1]);
}

Maintenant, je reçois un crash lors du défilement de la méthode du clicker maintenant à la ligne: NSLog(@"Volet 1: %@", [repPicker selectedRowInComponent:1]);

erreur:

* Fin de l'app en raison de uncaught exception 'NSRangeException', la raison:
'*
-[NSMutableArray objectAtIndex:]:
indice 1 au-delà des limites [0 .. 0]'

J'ai aussi eu Component 0: (null)

Mise à jour:

Code pour déclarer pickerArray:

pickerArray = [[NSMutableArray alloc] initWithCapacity:700];
for ( int i = 0 ; i <= 1000 ; ++i)
[pickerArray addObject:[NSString stringWithFormat:@"%d", i]];
pickerArrayHalf = [[NSMutableArray alloc]initWithCapacity:2];
[pickerArrayHalf addObject:@"0 lb"];
[pickerArrayHalf addObject:@"1/2 lb"];
  • Vous initWithCapacity:700 puis ajouter 1000 objets de la matrice.
InformationsquelleAutor | 2011-04-20