禁用自定义选择器中的特定行

发布于 2024-12-20 17:02:40 字数 199 浏览 4 评论 0原文

我正在开发一个应用程序,它显示具有以下值的选择器: 1.早上 2.中午 3.晚上

根据当前时间我需要显示相应的值。

如果是下午 3 点,则用户应该只能选择晚上。

现在我的问题是,有什么办法可以使“早上和中午”不可选择。类似于日期选择器,我可以将选项变灰吗?用户应该知道所有选项,但他应该能够根据时间选择选项。

提前致谢。

im developing an application which shows picker with the following values:
1.Morning
2.Noon
3.Evening

Based on the current time i need to show the respective values.

say if its 3PM the use should be able to select only Evening.

Now my questions is, is there any way i could make the "morning and Noon" unselectable. similar to Date picker can i grey out the options?? the user should be aware of all the options but he should be able to select option based on time.

thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

纵情客 2024-12-27 17:02:40

如果您想将选取器视图中的某些值灰显,请使用以下委托 API 来覆盖该位置的视图。

 - (UIView *)viewForRow:(NSInteger)row forComponent:(NSInteger)component

创建一个 UILabel 并将其文本颜色设置为灰色并将其返回为上述 UILabel 的视图。

如果选择了灰色文本,则通过调用以下 API 将动画设置为默认选择。

 - (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated;

希望这有帮助。

If you want to grey out some values in picker view, then make use of the following delegate API to override the view for that position.

 - (UIView *)viewForRow:(NSInteger)row forComponent:(NSInteger)component

Create a UILabel and make its text color to grey and return it as view for the above one.

And if the grey text is selected, animate to the default selection by calling the below API.

 - (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated;

Hope this helps.

谈情不如逗狗 2024-12-27 17:02:40

要使选取器中的行变灰,有一个比创建自己的视图更简单的解决方案。那就是使用attributedTitleForRow。

func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString?

因为如果返回 nil,该函数将回退到 titleForRow,所以您可以为禁用的行返回灰色,并使用标准 titleForRow 返回其余行。举个例子:

func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
    // Need to provide the below function isRowDisabled that returns true as needed
    if isRowDisabled(row:row) {
        let attributes = [NSAttributedStringKey.foregroundColor: UIColor.gray]
        let str = NSAttributedString(string: "my gray text on row \(row)", attributes:attributes)
        return str
    } else {
        return nil
    }
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    return "my normal text on row \(row)"
}

您必须忽略其他回复中提到的禁用行的 didSelectRow 。

To gray out rows in a Picker, there is a much easier solution than creating your own View. That is to use attributedTitleForRow.

func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString?

Because this function will fall back to titleForRow if you return nil, you can return gray for the disabled rows, and use the standard titleForRow to return the rest. As an example :

func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
    // Need to provide the below function isRowDisabled that returns true as needed
    if isRowDisabled(row:row) {
        let attributes = [NSAttributedStringKey.foregroundColor: UIColor.gray]
        let str = NSAttributedString(string: "my gray text on row \(row)", attributes:attributes)
        return str
    } else {
        return nil
    }
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    return "my normal text on row \(row)"
}

You'll have to then ignore the didSelectRow for the disabled rows as mentioned in the other replies.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文