2014-11-07 5 views
1

각 행에 두 개의 변수가 포함되어 있고 서버에서 데이터를 가져 오는 것을 통해 매 2 초마다 새로 고치기 때문에 TableView이 있습니다. 빨간색 또는 초록색이 높거나 낮 으면 모든 업데이트 색상을 변경하고 싶습니다. 그럼 내가 어떻게 할 수 있니? 라벨 색상을 변경하는 방법은 알고 있지만 각 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; 
} 
+0

당신이 당신의 데이터를 업데이트하는 코드를 공유하시기 바랍니다. –

+0

'data1' 배열에 대한 데이터를 업데이트하고 있습니다. 나는 코드 – CAN

+0

을 공유 할 것이다 또한 파싱 방법을 시작했다. – hasan83

답변

0

업데이트 :

이동 당신이 JSON을 구문 분석 시작하는 곳으로이 라인 data2 = data1;. 구문 분석에 사용하는 코드 앞에.

이전 게시물 :

- (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. 
} 
+2

나는'strOffer'와'strBid'를 비교하기를 원하지 않는다고 생각합니다. 2 초 전에'strOffer'의 값을'strOffer'의 값과 비교하려고합니다. –

+1

당신의 업데이트 된 대답은 당신이'dequeueReusableCellWithIdentifier'에서 얻은 행이 같은'indexPath'의 행이라고 가정합니다. 이것은 자주 사실이 아닙니다. –

+0

당신 말이 맞아요. 하지만 여기에있는 우리 친구는 오래된 데이터를 저장하지 않는 것 같습니다. 나는 다른 것을한다. – hasan83