如何将textlabel的颜色更改为UITableViewCellStyleValue1颜色

时间:2012-03-15 04:49:53

标签: objective-c xcode

这是一个noob问题。我在接口构建器中创建了一个表视图单元,并创建了一个表视图。我想要的是将textlabel的颜色更改为UITableViewCellStyleValue1颜色(浅蓝色)。因为我创建了单元格。 nib file.im无法使用cell.detailtextlabel.text.could你们帮助我。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[NSBundle mainBundle] loadNibNamed:@"CCell" owner:self options:nil] objectAtIndex:0];

}
UILabel *lbl=(UILabel*)[cell viewWithTag:1];
UIImageView *imgV = (UIImageView*)[cell viewWithTag:2];
UILabel *label=(UILabel*)[cell viewWithTag:3];

 NSDictionary *dToAccess = [self.listOfItems objectAtIndex:indexPath.row];
[lbl setText:[dToAccess valueForKey:@"title"]];
NSUInteger intVal = [[dToAccess valueForKey:@"rating"] integerValue];
switch (intVal) {
    case 0:
        [imgV setImage:[UIImage imageNamed:@"0star.png"]];
        [label setText:@"Snitt 0"];
        label.textColor=[UIColor blueColor];
        break;
    case 1:
        [imgV setImage:[UIImage imageNamed:@"1star.png"]];
        [label setText:@"Snitt 1"];
        break;
    case 2:
        [imgV setImage:[UIImage imageNamed:@"2star.png"]];
        [label setText:@"Snitt 2"];
        break;
    case 3:
        [imgV setImage:[UIImage imageNamed:@"3star.png"]];
         [label setText:@"Snitt 3"];
        break;
    case 4:
        [imgV setImage:[UIImage imageNamed:@"4star.png"]];
        [label setText:@"Snitt 4"];
        break;
    case 5:
        [imgV setImage:[UIImage imageNamed:@"5star.png"]];
        [label setText:@"Snitt 5"];
        break;
    default:
        break;
}
CGSize size = [lbl.text sizeWithFont:[UIFont boldSystemFontOfSize:16] forWidth:205 lineBreakMode:UILineBreakModeCharacterWrap];
[lbl setFrame:CGRectMake(5, 0, size.width, 43)];

[imgV setFrame:CGRectMake(5+size.width+5, 4, 118, 36)];

return cell;
}

2 个答案:

答案 0 :(得分:3)

您可以通过执行以下操作获取UITableViewCellStyleValue1的详细信息标签单元格颜色:

UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"resuse"];
UIColor *lightBlueColor = cell.detailTextLabel.textColor;

可以通过执行以下操作分解为RGB值和alpha:

const float* colors = CGColorGetComponents( lightBlueColor.CGColor );

如果您想对代码中的值进行硬编码或每次只计算它,这取决于您。我会说硬编码会更好。

答案 1 :(得分:0)

诀窍是掌握设置颜色的标签。如果您已经推出了自己的单元格,那么快速做的就是在标签中为标签添加标签值(与标签的其他视图属性一起查找)。

然后,当您配置单元格时,请获取如下标签:

UILabel *label = (UILabel *)[cell viewWithTag:16];  // 16 is a unique in the view, non-zero int that you make up yourself
label.textColor = [UIColor redColor];

另一种方法是将笔尖上的出口连接到自定义单元格上的属性,但标签可能是一种更简单的开始方式。