Question

I am trying to set the UIPicker View with the date for the last 20 Mondays. The array is filled with 20 Week Objects. e.g NSDate *weekDate=3/24/2014 NSString *weekDateString=Week of 3/24/2014.

When I set the rows initially it shows the first 5-6 rows correctly, when I try to scroll the picker is trying to set the title for the new rows it is displaying, but setTitle is logging (null) for the weekDateString. The list is still showing a count of 20, when I log the count in titleForRow.

If I set the NSArray just NSString there are no issues.

- (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;
}
Was it helpful?

Solution

There is nothing wrong with your code, so one is compelled to infer that there is probably something wrong with your model object, i.e. your _weeks array is not in fact correctly populated the way you think it is - the weekDateString is apparently missing for some of the Week objects. You should probably go back and look at how you are generating this array.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top