2012-03-15 16 views
0

これはnoob question.iのサブクラスです。インターフェイスビルダーのテーブルビューセルをサブクラス化し、テーブルビューを作成しました。テキストラベルの色をUITableViewCellStyleValue1の色青)。私は.nib file.imでセルを作成したので、cell.detailtextlabel.text.cを使用することができません。textlabelの色を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'の色はどういう意味ですか? –

+0

私は青色にlabel.textを設定しました。紺色です。色は薄い青色に変えてください(詳細は、テキストラベルにUITableViewCellStyleValue1を設定すると同様です)。 – stephensam

答えて

3

次の操作を行うことによりUITableViewCellStyleValue1ための詳細ラベルのセルの色を取得することができます:それは最高だ

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

行うことによって、それのRGB値とアルファに分けることができ
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"resuse"]; 
UIColor *lightBlueColor = cell.detailTextLabel.textColor; 

をコード内の値をハードコーディングしたい場合や、毎回計算する場合は、私はハードコードが良いと言うだろう。

+0

おかげでdatは完全に働いた... – stephensam

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

代替は、カスタムセルのプロパティに、あなたのペン先からコンセントを接続することですが、タグがする簡単な方法かもしれません始める。

関連する問題