Search code examples
uipickerviewuidatepickeruipicker

UIPickerView - How to make certain rows unselectable and grayed out


I am creating my own UIPickerview for dates as I need only Months and Days and do not want years, so I cannot use UIDatePicker. When different months are selected I cannot seem to replicate the way UIDatepicker gray's out the unavailable days. For example: when November is selected days 1-30 are selectable but 31 is not when using the UIDatepicker. Does anyone know how to replicate this and make certain row grayed out and unselectable. Thanks in advance


Solution

  • I realize you may have already figured this out, but I wanted to add some further information for those looking for the same answer.

    An alternative route that you might try is via:

    -(void)pickerView:(UIPickerView  *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
    

    In that method, you would check to see whether or not the particular components in the pickerView had a valid selection. If it didn't then reset the particular component (column - why didn't they just call it that) to a valid row with:

    [pickerView selectRow:validRow inComponent:component animated:YES];
    

    This will give the effect that if the user chooses a particular invalid row, it will scroll back up (or down) to a valid one.

    Additionally, you could change the dataset for the component in question and then do a:

    [pickerView reloadAllComponents];
    

    Just keep in mind that when you set up the picker, you'll have to do this validation on the first run so ensure that no invalid datasets are in the pickerView by default.