2009-10-11 5 views
0

j'ai ajouté une pickerview de l'interface builder.i am utilise cette méthode pour afficher des données comme celui-ci ....Le sélecteur affiche les numéros dans la sélection, mais ne s'affiche pas correctement sur l'étiquette.

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component 
{ 
if (pickerView == SpotPickerView) // don't show selection for the custom picker 
{ 
    // report the selection to the UI label 
label.text = [NSString stringWithFormat:@"%d - %@ - %d",[pickerView selectedRowInComponent:0], 
    [RowArray objectAtIndex:[pickerView selectedRowInComponent:1]], 
    [pickerView selectedRowInComponent:2]]; 
} 
} 


- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component 
{ 
NSString *returnStr = @""; 
if (pickerView == SpotPickerView) 
{ 
    if (component == 0) 
    { 
    returnStr = [[NSNumber numberWithInt:row+1] stringValue]; 
    } 
    else if(component==1) 
    { 
    returnStr = [RowArray objectAtIndex:row]; 
    } 
else 
{ 
    returnStr =[[NSNumber numberWithInt:row+1] stringValue]; 
    } 
} 

    return returnStr; 
} 

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component 
{ 
    CGFloat componentWidth = 0.0; 

    if (component == 0) 
    componentWidth = 90.0; 
    else if (component == 1) 
    componentWidth = 100.0; 
    else 
    componentWidth = 90.0; 

    return componentWidth; 
} 

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component 
{ 
    return 40.0; 
} 

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component 
{ 
    int count; 
    if (component == 0) 
    count = 15; 
    else if (component == 1) 
    count=[RowArray count]; 
else 
    count = 100; 

return count; 
} 

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView 
{ 
    return 3; 
} 

ici est le RowArray

- (void)viewDidLoad { 
RowArray = [[NSArray arrayWithObjects: 
    @"A", @"B", @"C", 
    @"D", @"E", @"F", @"G",@"H",@"I",@"J",@"K",@"L",@"M",@"N",@"O",@"P",@"Q",@"R",@"S",@"T", 
    @"U",@"V",@"W",@"X",@"Y",@"Z", 
    nil] retain]; 
} 

mais la sélection de la première ligne montre cette .

alt text http://i37.tinypic.com/66hphd.png

i besoin de stocker les valeurs dans la base de données donc j'ai besoin du 1 au lieu de zéro.

comment puis-je réinitialiser mes view.selects picker quand je clique sur un bouton, il vient automatiquement à l'index 0e dans chaque colonne.

Répondre

1
label.text = [NSString stringWithFormat:@"%d - %@ - %d",[pickerView selectedRowInComponent:0], 
    [RowArray objectAtIndex:[pickerView selectedRowInComponent:1]], 
    [pickerView selectedRowInComponent:2]]; 

Ce code récupère le numéro de ligne de la ligne sélectionnée dans un composant. Les tableaux sont indexés à zéro et la sélection (probablement) utilise des tableaux pour gérer les lignes. Ainsi, la première rangée sera 0, la deuxième 1, la troisième 2 et ainsi de suite. Ceci est facilement résolu en faisant:

[pickerView selectedRowInComponent:0] + 1

Vous pouvez sélectionner certaines lignes par programmation en utilisant selectRow:inComponent:animated Si vous voulez sélectionner l'index 0e dans chaque colonne le faire: [pickerView selectRow:0 inComponent:0 animated:YES] pour chaque composant.