UIPickerView:对同一个组件调用numberOfRowsInComponent两次,并且不显示行标题

时间:2011-06-07 15:11:34

标签: objective-c ios uipickerview

我正在尝试为我的应用程序设置登录表单,而UIPickerView没有显示行数据。我看到的是numberOfRowsInComponent被调用两次?但是titleForRow每行只被调用一次。

调试输出:

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 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

感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

我已经尝试过你的代码了,它运行正常。我可以在pickerView中看到两行,一行是美国,一行是加拿大 see the image below

确保您已正确设置委托和数据源。

答案 1 :(得分:0)

您的数据源委托方法可以被多次调用 - 这不是问题。你的代码是否正常工作?

相关问题