2013-12-18 116 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方法。