Redraw issue with UITableView using Objective C and Xcode 6

289 views Asked by At

I have a tableView which lists student names and highlights the row green for present or red for absent. If you click the row, the color is changed to reflect and change in status for the student. However, when a student is marked absent (highlighted red) and then I scroll the row out of view the row turns grey. I would love for the row to remain red (ugh). Here are the tableView methods

- (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];


}
1

There are 1 answers

0
Bouabane Mohamed Salah On BEST ANSWER

Try to set cell.backgroundView not cell.contentView.backgroundColor Or use cell.contentView.backgroundColor but you should note that the backgroundColor must be set in the tableView:willDisplayCell:forRowAtIndexPath: method (from UITableViewCell reference):