iOS,UIPickerView以编程方式创建,空视图,委托工作

时间:2013-12-04 02:20:41

标签: ios uipickerview

@Hey everybody,

我遇到了UIPickerView的问题。我打算创建一个视图控制器,允许用户指定第二天的视图控制器。 PickerView应显示“wed,1 Dec”,“thu,2 dec”等字符串 问题是PickerView为空(PickerView不显示任何字符串)。尽管委托方法返回必要的字符串数和字符串本身。

空PickerView的图像 - LINK

以下是我的代码。

·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);
}

和委托方法

// 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;
}

当我尝试旋转轮子时,有趣的事情“didSelectRow”被调用并始终返回row == 0并显示:

[55215:c07]你选择了这个[0,0]:星期三,04年12月

[55215:c07] count - 4

有什么想法吗?

另外:如果将相同的代码复制到某些UIViewController的内部函数的实现中,则相同的代码也能正常工作。例如,它是按钮事件处理程序,按下此按钮可调用代码。

它是什么??? *

PS:工作日志

[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

1 个答案:

答案 0 :(得分:0)

您可以添加/尝试:

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

 dPicker.showsSelectionIndicator = YES;

[self.view addSubview:dPicker];

 [dPicker reloadAllComponents]; // Should reload the data from what I understand