@嘿大家好,
我在使用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
.米
#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"被调用,总是返回行== 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
1条答案
按热度按时间omqzjyyz1#
您可以添加/尝试: