2012-03-15 196 views
0

這是一個noob question.i在界面構建器中對我的表視圖單元格進行了子類化,並創建了一個表視圖。我想要的是將文本標籤的顏色更改爲UITableViewCellStyleValue1顏色(light藍)。因爲我在.nib file.im中創建了單元格,所以無法使用cell.detailtextlabel.text.could你們幫我。如何將文本標籤的顏色更改爲UITableViewCellStyleValue1顏色

- (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; 
} 
+0

你能否澄清你的問題。你想改變'label.text'的顏色嗎? 「UITableViewCellStyleValue1」的顏色是什麼意思? – 2012-03-15 04:53:58

+0

我已經將label.text設置爲藍色。它給了我一個藏青色。想要的是將顏色更改爲淺藍色(設置UITableViewCellStyleValue1時的顏色類似於詳細的文本標籤)。 – stephensam 2012-03-15 04:57:28

回答

3

您可以通過執行獲得UITableViewCellStyleValue1細節標籤單元格顏色:

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

這取決於:

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

可細分爲它做的RGB值和Alpha如果你想要在代碼中硬編碼值或者每次只計算一次,你就可以使用它。我會說硬編碼會更好。

+0

謝謝dat完全工作... – stephensam 2012-03-15 05:31:36

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

另一種方法是從網點您榫連接性能上的自定義單元格,但標籤可能是一個更簡單的方法開始。