2014-01-28 3 views
0

섹션 번호를 기반으로 셀 배경색을 설정하려고하는데 올바르게 작동하지 않습니다. 셀 색상이 변경되고 잘못 스크롤됩니다. 나는 willDisplayHeaderView에 설정되어 있고 willDisplayCell에서 사용되는 색상 변수를 사용하여이 작업을 수행하려고합니다. 여기 코드는 : 사람이 하나 개의 큰 부분이 달성의 더 나은/쉬운 방법이있는 경우 다음을 제안 주시기 때문에섹션 당 셀 배경색을 설정할 수 있습니까?

또한
- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view  forSection:(NSInteger)section{ 

    if(section % 2 == 0){ 
     self.currentCellColor =[UIColor colorWithRed:(199/255.0) green:(214/255.0) blue:(156/255.0) alpha:1]; 
    } 
    else{ 
     self.currentCellColor = [UIColor colorWithRed:(242/255.0) green:(245/255.0) blue:(232/255.0) alpha:1]; 
    } 
} 

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { 
    cell.backgroundColor = self.currentCellColor; 
} 

섹션 기능이 작품에 매우 중요하지 않다. 색상을 4 줄씩 변경하면됩니다.

+0

당신이 cellForRowAtIndexPath'이 시도 않은이보십시오.? 'willDisplayCell :'대신에 – Akhilrajtr

+0

.? 또는 함께? 또는 다른 것? 나는 거기에 그것을 설정하려고했지만 여전히 스크롤에 망쳐. – RyanJohnstone

+0

'willDisplayCell'과'willDisplayHeaderView'의 인스트럭션 – Akhilrajtr

답변

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

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"identifier"]; 
    .... 

    if (indexPath.section % 2) {  
     cell.contentView.backgroundColor = [UIColor redColor]; 
    } 
    else { 
     cell.contentView.backgroundColor = [UIColor blueColor]; 
    } 

    return cell; 
} 
+0

나는 indexPath에서 섹션을 얻을 수 있다는 것을 몰랐습니다! – RyanJohnstone

0

만약 내가 각 섹션에 대한 다른 세포 식별자를 가지고 그 세포 색상을 설정했을 것입니다 cellForRowAtIndexPath: 그런 식으로 스크롤 및 tableView 당신의 세포를 재사 용할 때 당신은 그 하나를 잡는 것을 확신 할 수 있습니다 잘못된 색상. `:

0

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

    static NSString *cellIdentifier = @"cellIdentifier"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
    .... 

    if(indexPath.section % 2 == 0){ 
     self.currentCellColor =[UIColor colorWithRed:(199/255.0) green:(214/255.0) blue:(156/255.0) alpha:1]; 
    } 
    else{ 
     self.currentCellColor = [UIColor colorWithRed:(242/255.0) green:(245/255.0) blue:(232/255.0) alpha:1]; 
    } 
    cell.contentView.backgroundColor = self.currentCellColor; 

    .... 
    return cell; 
} 
관련 문제