2012-03-12 25 views
0

私は、テストの結果を表示するテーブルビューを持つ簡単なアプリケーションを作っています。結果は単純な配列から来ています。配列には数字だけがあり、テストのスコアは0〜100です。UITableViewセルの内容に基づいたセルの色

結果に応じて色を変更するためにUITableViewの行を取得しようとしています。 75以上は緑色の背景を表示し、> = 50 & & < 75が黄色、> 50が赤色になります。

これはこれまで私が行ってきたことです。私の理解は非常に基本的です。

- (UITableViewCell *)tableView:(UITableView *)tableView 
     cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    // Configure the cell... 
    // Set up the cell... 
    NSUInteger row = [indexPath row]; 
    cell.textLabel.text = [scoresArray objectAtIndex:row]; 

    // THIS IS WHERE I NEED HELP TO GET THE VALUE FROM THE ARRAY 
    // INTO ???? 

    if (???? >=75) { 
     cell.contentView.backgroundColor = [UIColor greenColor]; 
    } 
    if (???? >=50 && ???? <75) { 
     cell.contentView.backgroundColor = [UIColor yellowColor]; 
    } 
    if (???? >=0 && ???? <50) { 
     cell.contentView.backgroundColor = [UIColor redColor]; 
    } 
    else { 
     cell.contentView.backgroundColor = [UIColor whiteColor]; 
    } 

    return cell; 
} 

#pragma mark UITableViewDelegate 
- (void)tableView: (UITableView*)tableView willDisplayCell: 
(UITableViewCell*)cell forRowAtIndexPath: (NSIndexPath*)indexPath 
{ 
    cell.backgroundColor = cell.contentView.backgroundColor;  
} 

たとえば、cell.contentView.backgroundColor = [UIColor greenColor];を入力すると、すべて緑色になります。

答えて

0

、あなたはこれを使用することができます。

NSInteger score = [cell.textLabel.text intValue]; 

if (score >=75) { 
... 
+0

ありがとうございました。 –

0

これは整数を表す文字列ですか?その場合は、intValueを使用して整数に変換してください。それは浮動小数点を表す文字列ですか? floatValueを使用してください。あなたは配列に何が入っているかについて何の情報も与えていません。値がtextLabelで正しく表示されると仮定すると

+0

OKおかげで、配列内の数字だけがある - テストの点数。 99、78、34などのように入力します。 intValue = [scoresArray objectAtIndex:row]; ? –

+0

「数字だけ」という意味は? NSArrayに「単なる数字」を入れることはできません。オブジェクトは配列内にのみ置くことができます。どのような種類のオブジェクトですか?私はそれらがNSStringオブジェクトであると推測します。そうでない場合は、 'cell.textLabel.text = [scoresArray objectAtIndex:row]'と言うのは違法です。したがって、NSStringオブジェクトの場合は、整数として使用する必要があります。例えば、 '[[scoresArray objectAtIndex:row] intValue]'のように 'intValue'を使って整数に変換する必要があります。 NSStringドキュメント(NSStringオブジェクトの場合)を見てください。 – matt

関連する問題