如何在UIPickerView中更改所选行的颜色 [英] How to change color of selected row in UIPickerView

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

问题描述

好吧,也许我错过了一些非常简单的事情,如果是这种情况我会道歉,但是,我已经搜索了标题的每一个排列并且没有找到!所以这就是我想要做的事情:当选择该行时,将我正在使用的标签的背景颜色更改为2分量选择器视图中的行视图。所以我认为这样可行:

Ok, maybe I'm missing something really simple and I apologize if that's the case, however, I've googled every permutation of the title and have not found! So this is simply what I want to do: change the background color of the label I'm using as the row view in a 2 component pickerview when that row has been selected. So I thought this would work:

if (row == [pickerview selectedRowForComponent])
    viewlabel.backgroundColor = redcolor;

但这不起作用。它似乎随意选择要着色的行,有时甚至会给出错误的访问错误。我试过所有不同的条款都没有效果!非常感谢任何建议!!

but this doesn't work. It seems to arbitrarily choose which row to color and sometimes even give a bad access error. I've tried all different clauses to no effect! ANy suggestions would be greatly appreciated!!

以下是完整方法:

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

    if (component == kNumberComponent) {


#define PICKER_LABEL_FONT_SIZE 24
#define PICKER_LABEL_ALPHA 1.0
        // UIFont *font = [UIFont boldSystemFontOfSize:PICKER_LABEL_FONT_SIZE];
        UIFont *font = [ UIFont fontWithName:@"AppleGothic"  size:24];
        UILabel *carsLabel =[ [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 75, 50) ]autorelease];
        //[picker selectRow:row inComponent:component animated:YES];
        NSString *pickerText = [self.numbers objectAtIndex:(int)row];
        carsLabel.text = pickerText;
        carsLabel.textAlignment = UITextAlignmentCenter;
        NSLog(@"carsLabel = %@",carsLabel.text);
        //carsLabel.text = @"maybe because the string isn't long enough";
        carsLabel.textColor = [UIColor blackColor];
        carsLabel.font = font;





        carsLabel.opaque = YES;


        [view addSubview:carsLabel];

        return carsLabel;   
    } else {
        UIFont *font = [ UIFont fontWithName:@"AppleGothic"  size:18];

        UILabel *carsLabel = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 225, 50)] autorelease];
        id fact = [self.facts objectAtIndex:(int)row];
        NSString *pickerText = @"Dictionary Entry";
        if ( [fact isKindOfClass:[NSString class]]) {


            pickerText = [self.facts objectAtIndex:(int)row];

        } 
        carsLabel.text = pickerText;
        carsLabel.textAlignment = UITextAlignmentCenter;
        NSLog(@"carsLabel = %@",carsLabel.text);
        //carsLabel.text = @"maybe because the string isn't long enough";
        carsLabel.textColor = [UIColor blackColor];
        carsLabel.font = font;
        if ( row == 0) {
        carsLabel.backgroundColor = [UIColor redColor];

        }
        //carsLabel.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"blackboard.png"]];;
        carsLabel.opaque = YES;

        [view addSubview:carsLabel];

        return carsLabel;
    }


    return nil;
}


推荐答案

解决了!声明2个实例变量:selectedView和oldView。然后下面的代码可以解决这个问题:

Solved! Declare 2 instance variables: selectedView, and oldView. Then the following code does the trick:

if (self.oldView != nil)
        self.oldView.backgroundColor = [UIColor clearColor];

self.selectedView = [picker viewForRow:row forComponent:kNumberComponent];
        self.selectedView.backgroundColor = [UIColor redColor];
        [self.selectedView setNeedsDisplay];
        self.oldView = self.selectedView;

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

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