【发布时间】:2014-03-26 01:47:54
【问题描述】:
我正在尝试使用最近 20 个星期一的日期设置 UIPicker 视图。该数组填充了 20 个周对象。例如 NSDate *weekDate=3/24/2014 NSString *weekDateString=Week of 3/24/2014。
当我最初设置行时,它会正确显示前 5-6 行,当我尝试滚动选择器时,它会尝试为其显示的新行设置标题,但 setTitle 正在为 weekDateString 记录(null) .当我在 titleForRow 中记录计数时,列表仍显示计数为 20。
如果我将 NSArray 设置为 NSString 就没有问题。
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
_weeks = [Week getWeeks];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.title=@"Weeks";
[self.navigationController.navigationBar.layer insertSublayer:[LayoutColors getNavBarGradient:self.navigationController.navigationBar.bounds] atIndex:1];
self.navigationController.navigationBar.titleTextAttributes
= @{NSForegroundColorAttributeName : [UIColor whiteColor]};
int startYValue = self.navigationController.navigationBar.frame.size.height + HeaderHeight;
int width = self.view.frame.size.width;
_picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, startYValue, width/2, DATE_PICKER_HEIGHT)];
_picker.delegate = self;
_picker.dataSource = self;
[self.view addSubview:self.picker];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
//One column
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
//set number of rows
return _weeks.count;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
Week *week = [_weeks objectAtIndex:row];
return week.weekDateString;
}
【问题讨论】:
标签: ios ios7 uipickerview