@大家好,
我在使用 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]:12 月 4 日星期三
[55215:c07] 计数 - 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
您可以添加/尝试:
[dPicker setDataSource: self];
[dPicker setDelegate: self];
dPicker.showsSelectionIndicator = YES;
[self.view addSubview:dPicker];
[dPicker reloadAllComponents]; // Should reload the data from what I understand