0

this is a noob question.i subclassed my table view cell in interface builder and created a table view.what i want is to change the color of the textlabel to UITableViewCellStyleValue1 color(light blue).since i created the cell in .nib file.i m not able to use cell.detailtextlabel.text.could u guys help me out.

- (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
  • can you clarify your question. are you trying to change the color of your label.text? What do you mean to the color of UITableViewCellStyleValue1?
    – Joey J
    Mar 15, 2012 at 4:53
  • i ve set label.text as blue color.which gives me a navy blue..what is want is to change the color to light blue(color similar when set UITableViewCellStyleValue1 in detail textlabel).
    – stephensam
    Mar 15, 2012 at 4:57

2 Answers 2

3

You can get the detail label cell color for UITableViewCellStyleValue1 by doing:

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

which can be broken down into it's RGB values and alpha by doing:

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

It's up to you if you want to hardcode the values in code or just compute it every time. I'd say hardcode would be better.

0
0

The trick is getting hold of the label on which to set the color. If you've rolled your own cell, a quick thing to do is give your label a tag value in the nib, (look for it along with the other view properties of your label).

Then when you're configuring the cell, get the label like this:

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

The alternative is to connect outlets from your nib to properties on a custom cell, but tags might be a simpler way to start out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.