来自NSArray的UIView backgroundColor

时间:2014-04-08 20:11:00

标签: objective-c

我有一个UIScrollView,我想添加不同颜色的UIViews。

我在NSArray中声明了我的UIColors,现在应用程序崩溃了。

for (int x = 0; x < clrs; x++) {

    /// create the thumbnail size
    CGRect rect = clrCell.frame;
    rect.size.height = 40;
    rect.size.width = 40;
    rect.origin.x = cx;
    rect.origin.y = 0;

    /// add some properties to thumbnail image
    clrCell = [[UIView alloc] initWithFrame:rect];
    clrCell.backgroundColor = [colorPack objectAtIndex:x];

    [clrCell setUserInteractionEnabled:YES];
    clrCell.tag = x;

    UITapGestureRecognizer *singleTap =
    [[UITapGestureRecognizer alloc] initWithTarget:self
                                            action:@selector(handleSingleTap:)];
    singleTap.numberOfTapsRequired = 1;
    [clrCell addGestureRecognizer:singleTap];


    [_clrsVIEW addSubview:clrCell];

    cx += clrCell.frame.size.width+1;


}

这是我的NSArray:

 colorPack = [[NSArray alloc] initWithObjects:@"[UIColor colorWithRed:0.0f/255.0f green:0.0f/255.0f blue:0.0f/255.0f alpha:1.0];", @"[UIColor colorWithRed:255.0f/255.0f green:255.0f/255.0f blue:255.0f/255.0f alpha:1.0];", nil];

任何想法,如何从NSArray为UIView设置背景颜色?

1 个答案:

答案 0 :(得分:0)

您当前的方法是将颜色保存为字符串,这是一种方式:

colors = @[[UIColor colorWithRed:0.0f/255.0f green:0.0f/255.0f blue:0.0f/255.0f alpha:1.0], [UIColor colorWithRed:255.0f/255.0f green:255.0f/255.0f blue:255.0f/255.0f alpha:1.0] ];

我倾向于选择数组文字语法,但是如果你想坚持现有的方式:

colorPack = [[NSArray alloc] initWithObjects:[UIColor colorWithRed:0.0f/255.0f green:0.0f/255.0f blue:0.0f/255.0f alpha:1.0], [UIColor colorWithRed:255.0f/255.0f green:255.0f/255.0f blue:255.0f/255.0f alpha:1.0], nil];