(iOS) 如何使 UIPickerView 中的第一个值不可选择? [英] (iOS) How to make first value in UIPickerView unselectable?

查看:25
本文介绍了(iOS) 如何使 UIPickerView 中的第一个值不可选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

谢谢.问候.

推荐答案

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

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

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

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

这篇关于(iOS) 如何使 UIPickerView 中的第一个值不可选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆