Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

@Hey everybody,

I have trouble w/ UIPickerView. I'm planning to create a view controller which should allow user to specify the one of the next day. PickerView should show strings like "wed, 1 Dec", "thu, 2 dec", etc The problem is PickerView is empty (PickerView doesn't show any string). In spite of delegates methods return necessary count of strings and the strings themselves.

Image of empty PickerView - LINK

Below is my code.

.h

@interface OMSDatePickerViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource>

@property (nonatomic        ) NSInteger     amountOfDays;
@property (nonatomic        ) CGFloat       cellHeight;

@end

.m

#import "OMSDatePickerViewController.h"

@interface OMSDatePickerViewController () {
    NSMutableArray * dateForChoose;
    NSInteger datePickerActiveIdx;

    UIPickerView *dPicker;
}

@end

- (void)viewDidLoad
{
    NSLog(@"viewDidLoad");

    [super viewDidLoad];
    // Do any additional setup after loading the view.

    dateForChoose = [[NSMutableArray alloc] init];

    dPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(5, 5, 200, 150)];  

    [self.view setFrame: CGRectMake(xPos, yPos, WIN_WIDTH, WIN_HEIGHT)];
    [self.view setAlpha:1.0];
    [self.view setBackgroundColor:[UIColor grayColor]];
    [[self.view layer] setCornerRadius:5.0f];
    [[self.view layer] setBorderWidth:2.0f];
    [[self.view layer] setMasksToBounds:YES];
    [self.view setContentMode:UIViewContentModeScaleToFill];

    // Creating the list of dates
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    df.dateFormat = @"EEE',' dd MMM";

    [dateForChoose removeAllObjects];

    // Add some data for demo purposes.
    NSDate *curDate = [NSDate date];
    NSString *str;
    for (NSInteger i = 0; i< amountOfDays; i++, curDate = [curDate dateByAddingTimeInterval:60*60*24]) {
        str = [df stringFromDate:curDate];
        [dateForChoose addObject:str];
    }


    [dPicker setDataSource: self];
    [dPicker setDelegate: self];

    dPicker.showsSelectionIndicator = YES;

    [self.view addSubview:dPicker];


    datePickerActiveIdx = 0;

    NSLog(@"%@", dPicker);
}

and delegates methods

// Number of components.
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

// Total rows in our component.
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    NSLog(@"numberOfRowsInComponent: %d", [dateForChoose count]);
    return [dateForChoose count];
}

// Display each row's data.
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    NSString *str = [dateForChoose objectAtIndex: row];
    NSLog(@"%@", [dateForChoose objectAtIndex: row]);
    return [dateForChoose objectAtIndex: row];
}

// Do something with the selected row.
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    NSLog(@"You selected this [%2d, %2d]: %@", row, component, [dateForChoose objectAtIndex: row]);
    NSLog(@"count - %d", [dateForChoose count]);
    datePickerActiveIdx = row;
}

Interesting things when I try to rotate wheel "didSelectRow" is called and always return row == 0 and show:

[55215:c07] You selected this [ 0, 0]: Wed, 04 Dec

[55215:c07] count - 4

Any ideas?

Addition: the same code work well if it's copied to the implementation of internal function of some UIViewController. For example it's button event handler and the code is called by pressing this button.

WHAT IS IT???*

PS: working log

[18241:907] viewDidLoad
[18241:907] <UIPickerView: 0x1cd944a0; frame = (5 5; 190 162); layer = <CALayer: 0x1cd94990>>
[18241:907] viewWillAppear
[18241:907] numberOfRowsInComponent: 4
[18241:907] numberOfRowsInComponent: 4
[18241:907] Wed, 04 Dec
[18241:907] Thu, 05 Dec
[18241:907] Fri, 06 Dec
[18241:907] Sat, 07 Dec
[18241:907] viewDidAppear
share|improve this question
    
Do you see the log output from numberOfRowsInComponent: and titleForRow:? –  rmaddy Dec 4 '13 at 3:38
    
@rmaddy Yes, all delegates are called and seem to work well. I add log . –  user3124812 Dec 4 '13 at 3:52
    
Your viewDidLoad is full of suspicious code. Comment out all of the "layer" code. Also, don't set the frame of the view controller's view. That's never good. And lastly, don't use the initWithFrame: method to create the picker. Just use init so it defaults to the proper size. See if all of that helps. –  rmaddy Dec 4 '13 at 3:55
    
@rmaddy. Sorry, but there are not suspicious. Meanwhile these do not help too. Situation the same. –  user3124812 Dec 4 '13 at 4:00
    
By "suspicious" I meant the code looked like possible candidates for the cause of the problem. Your picker code looks right. The log output is correct. It makes no sense that the picker looks empty. But really, why are you setting the frame of self.view? That's very unusual. –  rmaddy Dec 4 '13 at 4:05

1 Answer 1

You can add/try:

[dPicker setDataSource: self];
[dPicker setDelegate: self];

 dPicker.showsSelectionIndicator = YES;

[self.view addSubview:dPicker];

 [dPicker reloadAllComponents]; // Should reload the data from what I understand
share|improve this answer
    
Sorry, it's not the reason. I've already tried it. Also reloading is necessary when data array is changed. –  user3124812 Dec 4 '13 at 2:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.