在我的xcode中,我有一个动态的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;
}
下面提到的代码是我有这个视图的表视图,对于tableview的每个单元格,我需要更改这个UiView的颜色。我知道它有点令人困惑,所以如果有人能帮助我,那将是可以理解的。
由于
答案 0 :(得分: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;
}
答案 1 :(得分: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];
答案 2 :(得分: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
在重用单元格中的正确用法
答案 3 :(得分: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];
答案 4 :(得分: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]];
答案 5 :(得分:0)
将忽略对cellForRowAtIndexPath中单元格的背景颜色的任何更改。而是改变单元格颜色:
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
cell.backgroundColor = [self bcgColorForIndexPath:indexPath];
}
...使用Greg的bcgColorForIndexPath方法。
答案 6 :(得分:-1)
只需将一些UIColor
实例化为一系列颜色:
NSMutableArray *myColors = [[NSMutableArray alloc] initWithObjects:[UIColor redColor], [UIColor blackColor], [UIColor pinkColor], [UIColor yellowColor] , nil];
然后将UIView
backgroundColor设置如下:
gaping.backgroundColor= [myColors objectAtIndex:indexPath.row];
如果它解决了你的目的,请告诉我。 请随意进一步询问。