【问题标题】:(iOS) How to make first value in UIPickerView unselectable?(iOS) 如何使 UIPickerView 中的第一个值无法选择?
【发布时间】:2014-03-26 11:07:25
【问题描述】:

我正在使用(例如)4 行的 UIPickerView。第一个值是带有灰色文本的“选择一个值”。其他是用户应该用黑色文本选择的值。

选择是可选的,因此用户可以跳过它。但如果他们不这样做,如何在用户开始选择后使第一个值无法选择?

谢谢。 问候。

【问题讨论】:

    标签: ios uipickerview uipicker


    【解决方案1】:

    如果选择了第一行,则使用 UIPickerView 委托方法- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component 更改所选行:

    - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    
        if (row == 0) {
            [pickerView selectRow:row+1 inComponent:component animated:YES];
        }
    }
    

    编辑:您可以使用此更改文本颜色:

    - (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
        }
    }
    

    如果您的目标是 iOS 6+,它会替换之前使用的 -pickerView:titleForRow:forComponent: 方法。

    【讨论】:

    • 它有效。还有一个问题 - 如何在该方法中使其变灰?如何自定义一行文字和颜色?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多