2011-06-07 35 views
2

我想爲我的應用程序設置登錄窗體,它是UIPickerView沒有顯示行數據。我所看到的是numberOfRowsInComponent被調用兩次?但是titleForRow只被每行調用一次。UIPickerView:numberOfRowsInComponent被調用兩次相同的組件,並沒有顯示行標題

調試輸出:

2011-06-07 11:07:31.096 myECG[19689:207] numberOfRowsInComponent: 2 : 0 
2011-06-07 11:07:31.096 myECG[19689:207] numberOfRowsInComponent: 2 : 0 
2011-06-07 11:07:31.098 myECG[19689:207] USA : 0 
2011-06-07 11:07:31.098 myECG[19689:207] Canada : 0 

LoginViewController.h

#import <UIKit/UIKit.h> 


@interface LoginViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate> { 
IBOutlet UITextField    *emailField; 
IBOutlet UITextField    *passswordField; 
IBOutlet UIButton     *loginButton; 
IBOutlet UIActivityIndicatorView *loginIndicator; 
IBOutlet UIPickerView    *pickerView; 
NSMutableArray      *sites; 

} 

@property (nonatomic, retain) UITextField    *emailField; 
@property (nonatomic, retain) UITextField    *passwordField; 
@property (nonatomic, retain) UIButton     *loginButton; 
@property (nonatomic, retain) UIActivityIndicatorView *loginIndicator; 
@property (nonatomic, retain) UIPickerView    *pickerView; 
@property (nonatomic, retain) NSMutableArray   *sites; 

-(IBAction) login:(id) sender; 
@end 

LoginViewController.m #進口 「LoginViewController.h」 #進口 「myECGViewController.h」

@implementation LoginViewController 
@synthesize emailField, passwordField, loginButton, loginIndicator, pickerView, sites; 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 

    } 

    //NSLog(@"%@", sites); 
    return self; 
} 

- (void)dealloc 
{ 
    [super dealloc]; 
} 

- (void)didReceiveMemoryWarning 
{ 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc that aren't in use. 
} 

#pragma mark - View lifecycle 

- (void)viewDidLoad 
{ 

    [super viewDidLoad]; 
    sites = [[NSMutableArray alloc] init]; 
    [sites addObject:@"Canada"]; 
    [sites addObject:@"USA"]; 

    //[pickerView selectRow:1 inComponent:0 animated:YES]; 
    // Do any additional setup after loading the view from its nib. 
} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    // Return YES for supported orientations 
    return NO; 
} 
-(IBAction) login:(id) sender 
{ 
    loginIndicator.hidden = FALSE; 
    [loginIndicator startAnimating]; 
    loginButton.enabled = FALSE; 
    // Do login 
    NSLog(@"E:%@ P:%@", emailField.text, passswordField.text); 
} 

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView 
{ 
    return 1; 
} 

-(NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component 
{ 
    NSLog(@"%@ : %d", [sites objectAtIndex:row], component); 
    return [sites objectAtIndex:row]; 
} 
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component 
{ 
    NSLog(@"numberOfRowsInComponent: %d : %d", [sites count], component); 
    return [sites count]; 
} 
-(void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component 
{ 
    NSLog(@"Selected: %@", [sites objectAtIndex:row]); 
} 
@end 

任何幫助表示讚賞。

回答

1

我試過你的代碼,它工作正常。我可以在picker中看到兩行查看一個是USA,一個是加拿大 see the image below

確保您已正確設置委託和數據源。

0

您的數據源委託方法可以被調用任意次數 - 這不是問題。你的代碼是否正確工作?