簡體   English   中英

使用Objective C和Xcode 6重繪UITableView的問題

[英]Redraw issue with UITableView using Objective C and Xcode 6

我有一個tableView,其中列出了學生的姓名,並突出顯示了綠色(當前)或紅色(缺席)。 如果單擊該行,則會更改顏色以反映和更改學生的狀態。 但是,當學生被標記為缺席(紅色突出顯示),然后我將其滾動到視圖之外時,該行變為灰色。 我希望該行保持紅色(ugh)。 這是tableView方法

- (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參考 ):

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM