Ios UITableView滚动查询时单元格颜色的变化

Ios UITableView滚动查询时单元格颜色的变化,ios,uitableview,Ios,Uitableview,在我的应用程序中,我在NSMutableArray和tableView行上显示一些问题。 我在每个问题下使用了两个按钮(tableView行按钮)。按YES(是)和NO(否)按钮,TableRow的颜色分别变为绿色、黄色 问题是在按下特定行的按钮后,该行变为绿色。但是在滚动之后,不需要的单元格也会变成绿色/黄色 你能检查我下面的代码并提出建议吗 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPat

在我的应用程序中,我在NSMutableArray和tableView行上显示一些问题。 我在每个问题下使用了两个按钮(tableView行按钮)。按YES(是)和NO(否)按钮,TableRow的颜色分别变为绿色、黄色

问题是在按下特定行的按钮后,该行变为绿色。但是在滚动之后,不需要的单元格也会变成绿色/黄色

你能检查我下面的代码并提出建议吗

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

contentView.hidden = YES;
if( tableView == myChklist)
{

    static NSUInteger const kQuestLabelTag = 1;
    static NSUInteger const kYesButtonTag = 2;
    static NSUInteger const kNoButtonTag = 3;

    UILabel *QuestLabel = nil;
    UIButton *YesButton;
    UIButton *NoButton;

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];


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

        cell.selectionStyle = UITableViewCellSelectionStyleNone;           //cell bg
        //self.myChklist.backgroundColor = [UIColor clearColor];
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
        {

            QuestLabel = [[UILabel alloc] initWithFrame:CGRectMake(5,5,768,0)];

           //some logic code
            [cell.contentView addSubview:QuestLabel];


            NSString *titleString = [buildingQuest objectAtIndex:indexPath.row] ;
            CGSize titleSize = [titleString sizeWithFont:[UIFont boldSystemFontOfSize:18] constrainedToSize:CGSizeMake(670, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];

            UIButton *YesButton = [UIButton buttonWithType:UIButtonTypeCustom];

                          //some logic button code

            [cell.contentView addSubview:YesButton];

            UIButton *NoButton = [UIButton buttonWithType:UIButtonTypeCustom];
            //some logic button code

            [cell.contentView addSubview:NoButton];



    }
    else

    {

        QuestLabel = (UILabel *)[cell.contentView viewWithTag:kQuestLabelTag];
        YesButton = (UIButton *)[cell.contentView viewWithTag:kYesButtonTag];
       NoButton = (UIButton *)[cell.contentView viewWithTag:kNoButtonTag];


    }


    QuestLabel.text = [buildingQuest objectAtIndex:indexPath.row];

    QuestLabel.numberOfLines = 0;

    NSString *titleString = [buildingQuest objectAtIndex:indexPath.row] ;
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
    {
        CGSize titleSize = [titleString sizeWithFont:[UIFont boldSystemFontOfSize:18] constrainedToSize:CGSizeMake(670, MAXFLOAT) lineBreakMode:NSLineBreakByWordWrapping];
        CGRect rect = QuestLabel.frame;
        rect.size.height = titleSize.height+10;
        QuestLabel.frame = rect;

        YesButton.frame = CGRectMake(10,titleSize.height+15,60,30);
        NoButton.frame  = CGRectMake(95,titleSize.height+15,60,30);

    }


if([self getCheckedForIndex:indexPath.row]==YES)
    {
        cell.backgroundColor = [UIColor greenColor];
        Cam_Button.hidden = NO;

    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }


    if([self getCheckedForYellow_Index:indexPath.row]==YES)
    {
        cell.backgroundColor = [UIColor yellowColor];
    }
    else
    {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }


    return cell;

    }
 }

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

UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];


if([self getCheckedForIndex:indexPath.row]==YES)
{
    cell.backgroundColor = [UIColor greenColor];
    Cam_Button.hidden = NO;

}
if([self getCheckedForYellow_Index:indexPath.row]==YES)
{
    cell.backgroundColor = [UIColor yellowColor];
}


 if ([self.arrayCheckUnchek containsObject:indexPath])
{
    cell.backgroundColor = [UIColor greenColor];
    Cam_Button.hidden = NO;

}

    if ([arrayCheckUnchek2 containsObject:indexPath])
    {
        cell.backgroundColor = [UIColor yellowColor];
    }

 }



 - (void)tableView: (UITableView*)tableView
willDisplayCell: (UITableViewCell*)cell
 forRowAtIndexPath: (NSIndexPath*)indexPath
 {
UITableViewCell *Cell = [myChklist cellForRowAtIndexPath:indexPath];
//same code as didSelectRowAtIndexPath method

}



-(void)NoAction:(id)sender event: (id)event
{
_cellButtonPress = YES;
checkUncheck = YES;
NSSet *touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint currentTouchPosition = [touch locationInView:self.myChklist];
NSIndexPath *indexPath = [self.myChklist indexPathForRowAtPoint: currentTouchPosition];
 UIButton *button = (UIButton *)sender;
UITableViewCell *cell = (UITableViewCell *)button.superview.superview;

cell.backgroundColor = [UIColor yellowColor];
[self checkedCellAtYellow_Index:indexPath.row];

[arrayCheckUnchek2 addObject:indexPath];
}

您需要设置新分配单元格的背景色。如果你不这样做,那么当已经变成绿色的细胞被重复使用时,它们就会保持绿色

    if (cell == nil) {
        cell.backgroundColor = [UIColor clearColor];
        ...

您需要设置新分配单元格的背景色。如果你不这样做,那么当已经变成绿色的细胞被重复使用时,它们就会保持绿色

    if (cell == nil) {
        cell.backgroundColor = [UIColor clearColor];
        ...

我做了…还没有成功。它完成了:)您的解决方案+在按钮操作中设置[self.myChecklist reloadData]。我做了…还没有成功。它完成了:)您的解决方案+在按钮操作中设置[self.myChecklist reloadData]。