2014-11-24 1 views
0

학생 이름을 나열하고 현재 행은 녹색으로 강조 표시하거나 부재 중일 경우 빨간색을 강조 표시하는 tableView가 있습니다. 행을 클릭하면 색상이 변경되어 학생의 상태가 반영되고 변경됩니다. 그러나 학생이 결석으로 표시되면 (빨간색으로 강조 표시) 행을 스크롤하여보기가 회색으로 바뀝니다. 나는 그 행이 빨갛게 (우) 남아 있기를 바랍니다. 여기 는Objective C 및 Xcode 6을 사용하여 UITableView에서 다시 그리기 문제

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 

    // Return the number of rows in the section. 
    return [kidsNames count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"myCell"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell == nil){ 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier]; 

    } 

    cell.textLabel.text = [kidsNames objectAtIndex:(indexPath.row)]; 
    NSString * status=[kidsAbsent objectAtIndex:indexPath.row]; 
    if([status isEqualToString: @"Present"]==1){ 
     cell.contentView.backgroundColor = [UIColor greenColor]; 
    }else{ 
     cell.contentView.backgroundColor = [UIColor redColor]; 
    } 



    return cell; 
} 



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 

     NSString * ab=[kidsAbsent objectAtIndex:indexPath.row]; 
     static NSString *CellIdentifier = @"myCell"; 
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    cell = [tableView cellForRowAtIndexPath:indexPath]; 


    NSLog(@"CLICKED!"); 

    if([ab isEqualToString: @"Present"]==1){ 
     [kidsAbsent replaceObjectAtIndex:indexPath.row withObject:@"Absent"]; 
     cell.contentView.backgroundColor = [UIColor redColor]; 
     NSLog(@"Now Absent %@",[kidsNames objectAtIndex:(indexPath.row)]); 
    }else{ 
      cell.contentView.backgroundColor = [UIColor greenColor]; 
      [kidsAbsent replaceObjectAtIndex:indexPath.row withObject:@"Present"]; 
      NSLog(@"Now Present %@",[kidsNames objectAtIndex:(indexPath.row)]); 
    } 


    stuID=[kidsID objectAtIndex:indexPath.row]; 
    stuAB=[kidsAbsent objectAtIndex:indexPath.row]; 


} 


- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath { 

    NSString * ab=[kidsAbsent objectAtIndex:indexPath.row]; 
    static NSString *CellIdentifier = @"myCell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    cell = [tableView cellForRowAtIndexPath:indexPath]; 


    NSLog(@"CLICKED!"); 

    if([ab isEqualToString: @"Present"]==1){ 
     [kidsAbsent replaceObjectAtIndex:indexPath.row withObject:@"Absent"]; 
     cell.contentView.backgroundColor = [UIColor redColor]; 
      NSLog(@"Now Absent %@",[kidsNames objectAtIndex:(indexPath.row)]); 
    }else{ 
     cell.contentView.backgroundColor = [UIColor greenColor]; 
     [kidsAbsent replaceObjectAtIndex:indexPath.row withObject:@"Present"]; 
     NSLog(@"Now Present %@",[kidsNames objectAtIndex:(indexPath.row)]); 
    } 


    //send info to the web=============================================================================== 
    stuID=[kidsID objectAtIndex:indexPath.row]; 
    stuAB=[kidsAbsent objectAtIndex:indexPath.row]; 


} 

답변

0

시도하지 cell.contentView.backgroundColor cell.backgroundView을 설정 또는 cell.contentView.backgroundColor을 사용하는있는 tableView 방법하지만 당신은 backgroundColor로는 UITableViewCell reference에서합니다 (tableView:willDisplayCell:forRowAtIndexPath: 방법으로 설정해야합니다주의해야) :

관련 문제