简体   繁体   中英

Redraw issue with UITableView using Objective C and Xcode 6

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


}

尝试设置cell.backgroundView而不是cell.contentView.backgroundColor或使用cell.contentView.backgroundColor,但是您应该注意,backgroundColor必须在tableView:willDisplayCell:forRowAtIndexPath:方法中设置(来自UITableViewCell参考 ):

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM