uiview的随机颜色

Ron*_*Ron 3 iphone objective-c uiview ios

在我的xcode中,我有一个动态的uiview

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

我需要动态改变背景颜色.

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

}
Run Code Online (Sandbox Code Playgroud)

下面提到的代码是我在这个视图中的表视图,对于tableview的每个单元格,我需要更改这个UiView的颜色.我知道它有点令人困惑,所以如果有人可以帮助我,那将是可以理解的.

谢谢

Dha*_*ngh 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;

} 
Run Code Online (Sandbox Code Playgroud)