2012-03-12 154 views
0

我正在制作一个带有显示测试结果的tableview的简单应用程序。结果来自一个简单的数组。在阵列中只有数字,测试分数介于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

假设该值在为textLabel正确显示,您可以使用此:

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

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

谢谢你的帮助。 – 2012-03-17 06:11:18

0

这是一个表示整数的字符串吗?如果是这样,请使用intValue转换为整数。它是一个表示浮动的字符串吗?使用floatValue。您不会提供有关阵列中的内容的任何信息。

+0

好,谢谢,在阵列中有只是数字 - 考试成绩。 99,78,34等。所以我把 intValue = [scoresArray objectAtIndex:row]; ? – 2012-03-12 02:11:33

+0

你是什么意思“只是数字”?你不能把“数字”放在一个NSArray中。您只能将对象放入数组中。什么样的物体?我认为他们是NSString对象;如果它们不是,那么说'cell.textLabel.text = [scoresArray objectAtIndex:row]'是违法的。因此,如果它们是NSString对象,那么要将其作为一个整数使用,则必须将其转换为带有'intValue'的整数,例如'[[scoresArray objectAtIndex:row] intValue]'。查看NSString文档(如果这些是NSString对象)。 – matt 2012-03-12 02:35:53

相关问题