(iOS) How to make first value in UIPickerView unselectable?

偶尔善良 提交于 2020-01-06 15:39:15

问题


I'm using UIPickerView with (for example) 4 rows. The first value is "Pick a value" with gray text. The others are values the user should pick with black text.

The picking is optional, so users could just skip it. But if they don't, how to make first value unselectable back after user will start picking?

Thanks. Regards.


回答1:


Use the UIPickerView delegate method - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component to change the selected row if the first row is selected :

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

    if (row == 0) {
        [pickerView selectRow:row+1 inComponent:component animated:YES];
    }
}

Edit : you may be able to change the text color using this :

- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component {

    if (row == 0)
        return [[NSAttributedString alloc] initWithString:@"Pick a value" attributes:@{NSForegroundColorAttributeName:[UIColor lightGrayColor]}];
    else {
        // Return titles
    }
}

This is if you target iOS 6+, and it replaces the previously used -pickerView:titleForRow:forComponent: method.



来源:https://stackoverflow.com/questions/22659074/ios-how-to-make-first-value-in-uipickerview-unselectable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!