2013-03-11 21 views
5

Ho seguito un paio di tutorial su youtube oltre a consigli su questo aspetto, ma non riesco a ottenere che UIPickerView visualizzi i suoi dati. Sto compilando il raccoglitore usando tre array e ho pensato di aver implementato il codice correttamente, ma ovviamente qualcosa non va. Il selettore mostra correttamente il numero di elementi, il problema è che sono tutti visualizzati come '?'UIPickerView non visualizza dati

Ho provato a impostare autonomamente il delegato del raccoglitore, ma anche questo non ha funzionato. Grazie in anticipo.

Ecco il mio file h:

@interface BACcalcViewController : UIViewController<UIPickerViewDataSource, UIPickerViewDelegate> 
@property(strong, nonatomic) IBOutlet UIPickerView *personInfoPicker; 
@property(strong, nonatomic) NSArray *heightsArray; 
@property(strong, nonatomic) NSArray *weightsArray; 
@property(strong, nonatomic) NSArray *timeArray; 
@end 

Ecco il mio file .m:

@interface BACcalcViewController() <UIPickerViewDataSource, UIPickerViewDelegate> 

@end 

@implementation BACcalcViewController 


@synthesize personInfoPicker, heightsArray, weightsArray, timeArray; 
- (void)viewDidLoad 
{ 

[super viewDidLoad]; 

heightsArray = [NSArray arrayWithObjects: 
         @"5ft 0in", 
         @"5ft 1in", 
         @"5ft 2in", 
         @"5ft 3in", 
         @"5ft 4in", 
         @"5ft 5in", 
         @"5ft 6in", 
         @"5ft 7in", 
         @"5ft 8in", 
         @"5ft 9in", 
         @"5ft 10in", 
         @"5ft 11in", 
         @"6ft 1in", 
         @"6ft 2in", 
         @"6ft 3in", 
         @"6ft 4in", 
         @"6ft 5in", 
         @"6ft 6in", 
         @"6ft 7in", 
         @"6ft 8in", 
         nil]; 

weightsArray = [NSArray arrayWithObjects: 
        @"100", 
        @"110", 
        @"120", 
        @"130", 
        @"140", 
        @"150", 
        @"160", 
        @"170", 
        @"180", 
        @"190", 
        @"200", 
        @"210", 
        @"220", 
        @"230", 
        @"240", 
        @"250", 
        @"260", 
        nil]; 


timeArray = [NSArray arrayWithObjects: 
      @"1", 
      @"2", 
      @"3", 
      @"4", 
      @"5", 
      @"6", 
      nil]; 
} 



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


- (NSInteger)pickerView:(UIPickerView *)personInfoPicker numberOfRowsInComponent: (NSInteger)component 
{ 
    if (component == 0) 
     return [heightsArray count]; 
    else if (component == 1) 
     return [weightsArray count]; 
    else 
     return [timeArray count]; 
} 

- (NSString *)pickerview:(UIPickerView *)personInfoPicker titleForRow:(NSInteger)row forComponent:(NSInteger)component 
{  
    if (component == 0) 
     return [heightsArray objectAtIndex:row]; 
    else if (component == 1) 
     return [weightsArray objectAtIndex:row]; 
    else 
     return [timeArray objectAtIndex:row]; 
} 
@end 
+1

hai impostato il delegato e l'origine dati di UIPickerView su File Owner? – channi

+0

Sì, ho collegato tutte le parti IB nello storyboard proprio come i video di YouTube. – theJPE

+0

quindi prova la risposta @ Durgaprasad una volta che può essere d'aiuto – channi

risposta

18

È necessario impostare la vista di selezione dataSource e chiamare reloadAllComponents dopo aver popolato gli array.

self.personInfoPicker.delegate = self; 
self.personInfoPicker.dataSource = self; 
[self.personInfoPicker reloadAllComponents]; 
+0

Devi prima impostare 'delegate' e' dataSource', quindi chiamare 'reloadAllComponents'. Quindi hai capito bene il codice, ma non l'ordine. –

+0

Devo impostare l'origine dati con: personInfoPicker.dataSource = self; – theJPE

+0

Perché non ci provi? Ho aggiornato la mia risposta con il codice esatto di cui hai bisogno. –

2

provare inizializzare tutte e tre le arrarys nel metodo init.

1

Il tuo codice è a posto. Basta cambiare l'oggetto del nome "pickerView" nel metodo.

Sostituire

- (NSInteger)pickerView:(UIPickerView *)personInfoPicker numberOfRowsInComponent: (NSInteger)component 

By,

- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent: (NSInteger)component 

E,

- (NSInteger)pickerView:(UIPickerView *)personInfoPicker numberOfRowsInComponent: (NSInteger)component 

By,

- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent: (NSInteger)component 

Perché personInfoPicker è l'oggetto di PickerView che hai già utilizzato nel file .h, quindi entrerà in conflitto durante il caricamento dei dati per pickerView.

Speriamo che ti possa aiutare.

Grazie.

1

Credo che il codice è assolutamente corretto e si imposta datasource e delegate pure ma si dimentica di ricaricare uipickerview così in ultima delle viewdidload reload pickerview che risolve il problema

0

penso che si did't assegnato la matrice usata come fonte dati del selezionatore, ad esempio

NSMutableArray *_month_ary=[[NSMutableArray alloc]initWithObjects:@"01",@"02",@"03",@"04",@"05",@"06",@"07",@"08",@"09",@"10",@"11",@"12", nil]; 
2

si può provare questo codice di seguito.

`NSMutableArray *_month_ary=[[NSMutableArray alloc]initWithObjects:@"01",@"02",@"03",@"04",@"05",@"06",@"07",@"08",@"09",@"10",@"11",@"12", nil]; 
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView; 
{ 
    return 1; 
} 

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component 
{ 


    if ([pickerView isEqual:_month_picker_view]) { 
     return [_month_ary count]; 
    } 
    return 0; 
} 
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component 
{ 


    if ([pickerView isEqual:_month_picker_view]) 
    { 
     return [_month_ary objectAtIndex:row]; 
    } 
    return nil; 
} 
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component 
{ 


    if ([pickerView isEqual:_month_picker_view]) { 
     _Pay_Month_txt.text=[_month_ary objectAtIndex:row]; 
     deal_cridit_card_month_str = [_month_ary objectAtIndex:row]; 
    } 
}` 
1

Ho copiato il codice iniziale e ho trovato il bug ora.V'è un errore di battitura nel nome metodo delegato

invece di

- (NSString *)pickerview:(UIPickerView *)personInfoPicker titleForRow:(NSInteger)row forComponent:(NSInteger)component 

deve essere

- (NSString *)pickerView:(UIPickerView *)personInfoPicker titleForRow:(NSInteger)row forComponent:(NSInteger)component 

Se si scrive pickerView lo dichiara un nuovo metodo nella classe che non sarà mai chiamato . Così semplice ma piacevole comunque :-)

+0

È lo stesso identico codice. – CyberMew