如何从pickerview中更改颜色选择的拾取值? [英] How to Change Color selected Picked value from pickerview?

查看:151
本文介绍了如何从pickerview中更改颜色选择的拾取值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

pickerArray = [[NSArray arrayWithObjects:@"20", @"40", @"60",@"80", @"100", @"120", @"140", @"160", @"180", @"200",nil] retain];

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

        //On Selecting the component row
        if (component == 0) {

        } else if (component == 1) {

            [quantityPickerDelegate didChangeLabelText:[pickerArray objectAtIndex:row]];// delegate passing the selected value
             pickerLabel.textColor = [UIColor colorWithRed:0.0745 green:0.357 blue:1.0 alpha:1];

    }   


- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {

    //Picker view content added
    pickerLabel = [[UILabel alloc] init];
    pickerLabel.backgroundColor=[UIColor clearColor];

    if (component == 0)
    {
        [pickerLabel setText:@"Total"];
        pickerLabel.frame = CGRectMake(0.0, 200,220,44);
        pickerLabel.textColor = [UIColor colorWithRed:0.0745 green:0.357 blue:1.0 alpha:1];
    }

    else
    {
        pickerLabel = [[UILabel alloc] init];
        pickerLabel.backgroundColor=[UIColor clearColor];
        pickerLabel.frame = CGRectMake(0.0, 200,70,44);
        [pickerLabel setText:[pickerArray objectAtIndex:row]];
        pickerLabel.textColor = [UIColor blackColor];
    }

    [pickerLabel setTextAlignment:UITextAlignmentCenter];
    pickerLabel.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:20];
    pickerLabel.font = [UIFont boldSystemFontOfSize:20];

    //return [pickerLabel autorelease];

    return pickerLabel;
}

我正在尝试从 UIPickerView

我正在尝试更改它,但它不会完全影响其更改低于所选值的其他值。

I am trying to change it but its not effected perfectly its changing other values below the selected value.

推荐答案

这是不正确的。您应该重新加载所选组件。然后,您需要获取所选行并在 viewForRow:forComponent:中更新其颜色。类似于 -

This is incorrect. You should reload the selected component. Then you need to fetch the selected row and update its color in viewForRow:forComponent:. Something like-

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

        //On Selecting the component row
        if (component == 0) {

        } else if (component == 1) {

            [quantityPickerDelegate didChangeLabelText:[pickerArray objectAtIndex:row]];// delegate passing the selected value
            [pickerView reloadComponent:component]; //This will cause your viewForComp to hit
        }
    }

- (UIView *)viewForRow:(NSInteger)row forComponent:(NSInteger)component
{
     //...
     //Your usual code
      pickerLabel.textColor = defaultColor;

     if([self.pickerView selectedRowInComponent:component] == row) //this is the selected one, change its color
     {
            pickerLabel.textColor = [UIColor colorWithRed:0.0745 green:0.357 blue:1.0 alpha:1];
     } 
}

这篇关于如何从pickerview中更改颜色选择的拾取值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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