Ios 变量之间的比较&;更改标签颜色

Ios 变量之间的比较&;更改标签颜色,ios,Ios,我有一个TableView,每行包含两个变量,它通过从服务器获取数据每2秒刷新一次。我想改变标签的颜色每次更新,如果它是更高或更低的红色或绿色。那么,我该怎么做呢?虽然我知道如何更改标签颜色,但不知道如何在每2秒之间进行压缩 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier

我有一个
TableView
,每行包含两个变量,它通过从服务器获取数据每2秒刷新一次。我想改变标签的颜色每次更新,如果它是更高或更低的红色或绿色。那么,我该怎么做呢?虽然我知道如何更改标签颜色,但不知道如何在每2秒之间进行压缩

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

static NSString *CellIdentifier = @"comCell";

comTableViewCell *cell = (comTableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

if (cell == nil) {
    NSArray *parts = [[NSBundle mainBundle] loadNibNamed:@"comTableViewCell" owner:nil options:nil];
    cell = [parts objectAtIndex:0];
}

id rowObject = [data1 objectAtIndex:indexPath.row];

[cell.lblType setText:[rowObject objectForKey:@"strSymbol"]];
[cell.lblOffer setText:[rowObject objectForKey:@"strOffer"]];
[cell.lblBid setText:[rowObject objectForKey:@"strBid"]];

cell.lblType.font = [UIFont fontWithName:@"Helvetica" size:12.0f];
cell.lblOffer.font = [UIFont fontWithName:@"Helvetica" size:12.0f];
cell.lblBid.font = [UIFont fontWithName:@"Helvetica" size:12.0f];

cell.lblType.textColor = [UIColor blackColor];
cell.lblOffer.textColor = [UIColor whiteColor];
cell.lblBid.textColor = [UIColor whiteColor];


return cell;
}
数据结果:

        {
    strBid = "1.29158";
    strOffer = "1.29258";
    strSymbol = "USD/EUR";
},
    {
    strBid = "98.964";
    strOffer = "99.004";
    strSymbol = "AUD/JPY";
},
    {
    strBid = "11.2472";
    strOffer = "11.2972";
    strSymbol = "USD/ZAR";
}
更新:

    data2 = data1;

    id rowObject2 = [data2 objectAtIndex:indexPath.row];

    NSString *new_offer = [rowObject2 objectForKey:@"strOffer"];

    if (data2 != nil) {

        id old_rowObject = [data2 objectAtIndex:indexPath.row];

        NSString *old_offer = [old_rowObject objectForKey:@"strOffer"];

        if ([new_offer doubleValue] > [old_offer doubleValue]) {

            cell.lblOffer.textColor = [UIColor greenColor];
            cell.lblBid.textColor = [UIColor greenColor];
        }

        else if ([new_offer doubleValue] == [old_offer doubleValue]) {

            cell.lblOffer.textColor = [UIColor blackColor];
            cell.lblBid.textColor = [UIColor blackColor];
        }

        else {

            cell.lblOffer.textColor = [UIColor redColor];
            cell.lblBid.textColor = [UIColor redColor];
        }

    } else {
        // first time getting data.
    }

    return cell;
}

更新:

- (void)parserDidStartDocument:(NSXMLParser *)parser {
    old_data = data1;  <<-- saves data here!
    data1 = [[…. alloc] init]; 
}

id rowObject = [data1 objectAtIndex:indexPath.row];
NSString *new_offer = [rowObject objectForKey:@"strOffer"];

if (old_data != nil) {
    id old_rowObject = [old_data objectAtIndex:indexPath.row]; 
    NSString *old_offer = [old_rowObject objectForKey:@"strOffer"];

    if ([new_offer doubleValue] > [old_offer doubleValue]) {
     // set colour here.
    } else {
     // and here.
    }
} else {
    // first time getting data.
}
移动此行
data2=data1
到您开始解析json的位置。在您用来解析的代码之前

旧帖子:

- (void)parserDidStartDocument:(NSXMLParser *)parser {
    old_data = data1;  <<-- saves data here!
    data1 = [[…. alloc] init]; 
}

id rowObject = [data1 objectAtIndex:indexPath.row];
NSString *new_offer = [rowObject objectForKey:@"strOffer"];

if (old_data != nil) {
    id old_rowObject = [old_data objectAtIndex:indexPath.row]; 
    NSString *old_offer = [old_rowObject objectForKey:@"strOffer"];

    if ([new_offer doubleValue] > [old_offer doubleValue]) {
     // set colour here.
    } else {
     // and here.
    }
} else {
    // first time getting data.
}
-(void)parserdistartdocument:(NSXMLParser*)解析器{

old_data=data1;我认为他不想将
strOffer
strBid
进行比较。我认为他想将
strOffer
2秒前的值与
strOffer
现在的值进行比较。更新后的答案假设您从
dequeueReusableCellWithIdentifier
获得的行是来自同一
的行e> indexPath
,这通常是不正确的。你是对的。但是,看起来我们这里的朋友不存储旧数据。我做了其他事情。很抱歉@IanMacDonald是对的,我想将2秒前的strOffer值与现在的strOffer值进行比较。@如果要将旧数据与新数据进行比较,是否需要存储旧数据。请共享您更新数据的代码。我正在
data1
array上更新数据。我将共享代码以及did start解析方法。@hasan83您能告诉我保存数据和检查的最佳方法是什么吗?答案中有。在读取新数据之前,只需将旧数据放入另一个NSMUTABLEARRY中。如代码中所示。