如何在UIPickerView中更改所选行的颜色

时间:2009-05-21 23:45:45

标签: iphone uipickerview

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

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

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

这是完整的方法:

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

11 个答案:

答案 0 :(得分:11)

通常,我使用这种方法:

我使用自定义视图来显示行项目

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

    UILabel *label = (id)view;

    if (!label)
    {

        label= [[UILabel alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [pickerView rowSizeForComponent:component].width, [pickerView rowSizeForComponent:component].height)];
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = [UIColor whiteColor];
        label.text = _arrayStringPicker[row];

    }  

    return label;

我改变选择的行的颜色:

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

    UILabel *labelSelected = (UILabel*)[pickerView viewForRow:row forComponent:component];
    [labelSelected setTextColor:[UIColor redColor]];

}

答案 1 :(得分:6)

viewForPicker的正确格式为:

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UILabel *label = (UILabel*) view;
    if (label == nil)
    {
        label = [[UILabel alloc] init];
    }

    [label setText:@"Whatever"];

    // This part just colorizes everything, since you asked about that.

    [label setTextColor:[UIColor whiteColor]];
    [label setBackgroundColor:[UIColor blackColor]];
    CGSize rowSize = [pickerView rowSizeForComponent:component];
    CGRect labelRect = CGRectMake (0, 0, rowSize.width, rowSize.height);
    [label setFrame:labelRect];

    return label;
}

上面代码的问题是:它使标签着色,而不是选择器本身。因此,当您滚动到一端或另一端时,会有一个空白点,您可以看到白色背景。设置[myPicker setBackgroundColor ...]并不能达到人们所希望的效果。

答案 2 :(得分:4)

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

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;

答案 3 :(得分:4)

快速实施

@Test
public void testEnumCompletude() {
    for (BusinessCriticality c : BusinessCriticality.values()) {
        for (EmergencyLevel e : EmergencyLevel.values()) {
            assertNotNull(String.format("%s/%s combination was forgotten", c, e), 
                EmergencyPriority.of(c, e));
        }
    }
}

答案 4 :(得分:4)

//我们想要的框架的CGRectMake值

UIPickerView *myPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0,0, self.view.bounds.size.width, self.view.bounds.size.height)];

// add the UIPickerView to your viewcontroller [mainView addSubview:myPickerView];

// set the selectionindicator to none myPickerView.showsSelectionIndicator = NO;

//定义我们想要使用的图像

UIImage *selectorImage = [UIImage imageNamed:@"selectionIndicator.png"];

UIView *customSelector = [[UIImageView alloc] initWithImage:selectorImage];

//将x和y值设置为UIPickerView上要放置图像的位置

//将宽度和高度值设置为图像的宽度和高度

customSelector.frame = CGRectMake(10,(myPickerView.bounds.size.height / 2) + 16, self.view.bounds.size.width – 10, 47);

//将自定义selectionIndicator也添加到同一个viewcontroller

[mainView addSubview:customSelector];

答案 5 :(得分:2)

调用UIPickerView实例的-viewForRow:forComponent:方法获取UIView *对象。然后设置该视图的背景UIColor

答案 6 :(得分:0)

扩大亚历山德罗·皮罗瓦诺的答案;

if (auth()->attempt(array('name' => $uname , 'password' => $password ))){
   return "success";
} else {        
   return "Wrong Credentials";
}  

答案 7 :(得分:0)

目前还不清楚您将上述代码放在何处。它在-pickerView:viewForRow:forComponent:reusingView:吗?这应该是它应该的地方。您确定要维护指向该特定视图的标签的指针吗?你崩溃的事实暗示你可能不是。更大的代码块,包括您放置的代码,将会很有帮助。

答案 8 :(得分:0)

这对我有用:

  1. UIView作为子视图添加到UIPickerView
  2. 将其限制为使用UIPickerView输入Y并且宽度相同
  3. 将其高度等于pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat
  4. 中返回的高度
  5. 使其半透明并按照您想要的方式着色
  6. *其他解决方案(对于行的行或属性字符串的自定义视图)在快速滚动时有错误。

答案 9 :(得分:0)

@ alessandro-pirovano答案的快速版本

func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {

    let rowSize = pickerView.rowSize(forComponent: component)
    let width = rowSize.width
    let height = rowSize.height
    let frame = CGRect(x: 0, y: 0, width: width, height: height)
    let label = UILabel(frame: frame)
    label.textAlignment = .center
    label.text = rows[row]

    return label
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

    guard let label = pickerView.view(forRow: row, forComponent: component) as? UILabel else {
        return
    }
    label.backgroundColor = .orange
}

答案 10 :(得分:0)

UIPickerView有委托为行和组件设置NSAttributeString我们可以设置属性颜色如下:

- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component {
    NSDictionary *attrs = @{NSForegroundColorAttributeName : [UIColor redColor]};
    NSString *title = @"title"
    return [[NSAttributedString alloc] initWithString:title attributes:attrs];

}