2013-12-18 121 views
3

在我的Xcode我有一个UIView是动态随机颜色的UIView

UIView *gaping=[[UIView alloc] initWithFrame:CGRectMake(15, 0, 172, 39)]; 
    gaping.backgroundColor= [UIColor redColor]; 

我需要的背景色为动态变化。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    UIView *gaping=[[UIView alloc] initWithFrame:CGRectMake(15, 0, 172, 39)]; 
    gaping.backgroundColor= [UIColor redColor]; 


    UITableViewCell *TableCell = [[UITableViewCell alloc] init]; 
    NSMutableDictionary *RRTYRT = [DATAARRAY objectAtIndex:indexPath.row]; 

    UILabel *LabelOne = [[UILabel alloc] initWithFrame:CGRectMake(45, 0, 100, 30)]; 
    LabelOne.textColor = [UIColor whiteColor]; 
    LabelOne.backgroundColor = [UIColor clearColor]; 
    LabelOne.text = [RRTYRT objectForKey:@"themename"]; 
    [gaping addSubview:LabelOne]; 

    [gaping.layer setCornerRadius:5.8f]; 

    [TableCell.contentView addSubview:gaping]; 
    return TableCell; 

} 

下面提到的代码是在其内我有此视图和用于实现代码如下的每个小区i需要更改此UIView的颜色的表格视图。我知道它的理解有点混乱,所以如果任何人都可以帮助我的话,那将是不可取的。

感谢

+9

**非常非常糟糕的命名约定** –

+0

其中是随机颜色,对于每个cellview它具有相同的“白色”颜色。 –

+1

无法理解 – Sport

回答

9

尝试使用这样的,让你的颜色,你想要什么:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 

    UIView *gaping=[[UIView alloc] initWithFrame:CGRectMake(15, 0, 172, 39)]; 

    CGFloat redLevel = rand()/(float) RAND_MAX; 
    CGFloat greenLevel = rand()/(float) RAND_MAX; 
    CGFloat blueLevel = rand()/(float) RAND_MAX; 

    gaping.backgroundColor = [UIColor colorWithRed: redLevel 
               green: greenLevel 
               blue: blueLevel 
               alpha: 1.0]; 


    UITableViewCell *TableCell = [[UITableViewCell alloc] init]; 
    NSMutableDictionary *RRTYRT = [DATAARRAY objectAtIndex:indexPath.row]; 

    UILabel *LabelOne = [[UILabel alloc] initWithFrame:CGRectMake(45, 0, 100, 30)]; 
    LabelOne.textColor = [UIColor whiteColor]; 
    LabelOne.backgroundColor = [UIColor clearColor]; 
    LabelOne.text = [RRTYRT objectForKey:@"themename"]; 
    [gaping addSubview:LabelOne]; 

    [gaping.layer setCornerRadius:5.8f]; 

    [TableCell.contentView addSubview:gaping]; 
    return TableCell; 

} 
3
UIColor *color; 
float randomRed = arc4random() % 255; 
float randomGreen = arc4random() % 255; 
float randomBlue = arc4random() % 255; 

gaping.backgroundColor= [UIColor colorWithRed:randomRed green:randomGreen blue:randomBlue alpha:1.0]; 
3

这里是Tapku library

- (UIColor*) randomColor{ 
    int r = arc4random() % 255; 
    int g = arc4random() % 255; 
    int b = arc4random() % 255; 
    return [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1]; 
} 

And to use it: 
LabelOne.backgroundColor = [self randomColor]; 

随机颜色,但你应该考虑阅读一些关于在Objective-C中开发的文章,以及UITableView与reu的正确使用唱细胞

-1

只是一些实例来UIColor的颜色数组:

NSMutableArray *myColors = [[NSMutableArray alloc] initWithObjects:[UIColor redColor], [UIColor blackColor], [UIColor pinkColor], [UIColor yellowColor] , nil]; 

然后设置你的UIView的backgroundColor如下:

gaping.backgroundColor= [myColors objectAtIndex:indexPath.row]; 

能否解决你的目的,让我知道。 请随时询问。

+0

无法正常工作..帮助 – Saranjith

0

这是一个很好的方式来获得类似的随机背景颜色清除应用效果:

-(UIColor*)bcgColorForIndexPath:(NSIndexPath*) indexPath 
{ 
    NSUInteger rowCount = [YOURARRAY count] - 1; //it's your array which gives you all rows for data source 
    float value = ((float)indexPath.row/(float)rowCount) * 0.5; // You can change this value depends on your needs 
    return [UIColor colorWithRed: 1.0 green:value blue: 0.0 alpha:1.0]; // You can make different color dynamic, for example red, it's deepends on your requirements 
} 

而且你怎么称呼它那样:

gaping.backgroundColor = [self bcgColorForIndexPath:indexPath]; 
0

试试这个

int lowerBound = 1; 
int upperBound = 255; 
int red = lowerBound + arc4random() % (upperBound - lowerBound); 
int green = lowerBound + arc4random() % (upperBound - lowerBound); 
int blue = lowerBound + arc4random() % (upperBound - lowerBound); 
[gaping setBackgroundColor:[UIColor colorWithRed:red green:green blue:blue alpha:1.0]]; 
0

将单元格的背景颜色更改为cellForRowAtIndexPath将被忽略。改为改变单元格颜色:

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { 
    cell.backgroundColor = [self bcgColorForIndexPath:indexPath]; 
} 

...使用Greg的bcgColorForIndexPath方法。