21

好的,也许我错过了一些非常简单的东西,如果是这种情况,我深表歉意,但是,我已经用谷歌搜索了标题的所有排列,但没有找到!所以这就是我想要做的:在选择该行时更改我用作 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;
}
4

11 回答 11

10

通常,我使用这种方法:

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

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

}
于 2015-08-21T14:31:45.277 回答
7

快速实施

extension PickerViewController: UIPickerViewDelegate {
    func pickerView(pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
        var color: UIColor!
        if pickerView.selectedRowInComponent(component) == row {
            color = UIColor.redColor()
        } else {
            color = UIColor.blackColor()
        }

        let attributes: [String: AnyObject] = [
            NSForegroundColorAttributeName: color,
            NSFontAttributeName: UIFont.systemFontOfSize(15)
        ]

        return NSAttributedString(string: rows[row], attributes: attributes)
    }

    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        //this will trigger attributedTitleForRow-method to be called
        pickerView.reloadAllComponents()
    }
}
于 2016-04-05T08:45:23.197 回答
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...] 并不像人们希望的那样。

于 2009-09-12T18:55:07.400 回答
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 也添加到同一个视图控制器

[mainView addSubview:customSelector];
于 2013-06-05T06:20:33.367 回答
3

解决了!声明 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;
于 2009-05-22T18:18:41.530 回答
2

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

于 2009-05-22T00:14:31.623 回答
2

@alessandro-pirovano 答案的 Swift 版本

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
}
于 2019-02-20T10:35:15.460 回答
0

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

于 2009-05-22T00:09:03.070 回答
0

这对我有用:

  1. 将 aUIView作为子视图添加到您的UIPickerView
  2. 将其限制为与您输入的 YUIPickerView并且 具有相同的宽度
  3. 给它高度等于返回的高度pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat
  4. 使其半透明并按照您想要的方式着色

*其余的解决方案(行的自定义视图或行的属性字符串)在快速滚动时有问题。

于 2017-05-31T14:24:40.650 回答
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];

}

于 2018-04-28T07:37:37.510 回答
0

扩展 Alessandro Pirovano 的答案;

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

    UILabel *labelSelected = (UILabel*)[pickerView viewForRow:row forComponent:component];
    [labelSelected setTextColor:[UIColor redColor]];
    labelSelected.superview.backgroundColor = [UIColor yellowColor];
}
于 2020-12-12T09:00:11.827 回答