2014-10-10 2 views
0

배경색을 내 tableview의 각 행마다 다르게 변경하고 싶습니다. 예를 들어, 내 배열은 3,2,1입니다. 첫 번째 행이 내 '합계'(3)과 같으면 두 번째 행이 내 '합계'에서 1을 뺀 것이면 빨간색으로 원한다. (3) 파란색으로 원한다. 기준이 충족 될 경우에만 전체 tableview 색상을 변경할 수 있습니다.배열의 각 객체마다 셀 배경색을 다르게 설정하십시오.

if ([myArray containsObject:total]) {  
     cell.backgroundColor = [UIColor redColor]; 
     } 

    else if ([myArray containsObject:totalMinusOne]) { 
     cell.backgroundColor = [UIColor blueColor]; 
     } 

각 셀의 배경을 변경하고 if/else 문에 의존하게하려면 어떻게해야합니까?

+0

실제로 각 셀에 대해 서로 다른 총인가? 확인을 위해 중단 점을 두었습니까? – Nick

답변

1

당신이 필요 -

그것에 대해 UITableView 대리자 메서드를 사용

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { 
    switch (indexPath.row) { 
     case 0: 
      [cell setBackgroundColor:[UIColor redColor]]; 
      break; 

     case 1: 
      [cell setBackgroundColor:[UIColor greenColor]]; 
      break; 

     case 2: 
      [cell setBackgroundColor:[UIColor blueColor]]; 
      break; 

     default: 
      break; 
    } 
} 
0

셀의 ContentView 배경색을 설정해야합니다. 여기

[cell.contentView setBackgroundColor:[UIColor blueColor]]; 
+0

교체를 시도했지만 셀 색상이 동일하게 작용하지만 측면을 따라 수직입니다. – Ryasoh

+0

셀 위쪽에 다른보기가 있습니까? – sateesh

+0

아마 콘텐츠 뷰에 ​​라벨이 붙어있을 수 있습니다. 다른보기 배경을 [UIColor clearColor]로 설정해야합니다. – BoranA

관련 문제