开发者

UIPickerView: numberOfRowsInComponent is being called twice for same component and is not displaying row titles

开发者 https://www.devze.com 2023-03-10 20:16 出处:网络
I\'m trying to setup a login form for my application and it is the UIPickerView is not showing the row data. What I am seeing is that the numberOfRowsInComponent is being called twice? But titleForRow

I'm trying to setup a login form for my application and it is the UIPickerView is not showing the row data. What I am seeing is that the numberOfRowsInComponent is being called twice? But titleForRow is only being called once per row.

Debug output:

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 #import "LoginViewController.h" #import "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 forCo开发者_运维技巧mponent:(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

Any help is appreciated.


I've tried your code and it is working fine. I can see two rows in pickerView one is USA and one is Canada

UIPickerView: numberOfRowsInComponent is being called twice for same component and is not displaying row titles

Make Sure you have set delegate and datasource properly.


Your data source delegate methods can get called any number of times - that is not a problem. Is your code working correctly otherwise?

0

精彩评论

暂无评论...
验证码 换一张
取 消