0

我创建了自定义类,并且将所有文本颜色设置为黑色。选择行后,我将行内容的文本颜色更改为绿色。在向左滑动手势事件中,我再次重新加载了表格,但之前选择的行文本没有将他的颜色改回黑色,即使在表格重新加载后它仍然是绿色,但它的文本值正在改变。为什么它不把颜色变回黑色

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger rows=indexPath.row;
    if(rows==2||rows==3||rows==4)
    {
  UIImageView *localImageView=(UIImageView*)[self.view  viewWithTag:rows];
    localImageView.image=[UIImage imageNamed:@"check"];
    UILabel *localLabel=(UILabel*)[self.view viewWithTag:rows+100];

        Question *question=[[Question alloc]init];
        question=[self.questionsArray objectAtIndex:self.currentQuestionIndex];
        if([localLabel.text isEqualToString:question.correctOptionText])
        {
            localLabel.textColor=[UIColor greenColor];  //changed color here
        }
        else{
            localLabel.textColor=[UIColor redColor];

        }
    }
}

自定义类代码

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        UIImageView *localCheckImageView=[[UIImageView alloc]initWithFrame:CGRectMake(45, 10, 25, 25)];
        localCheckImageView.image=[UIImage imageNamed:@"dot"];
        self.checkImageView=localCheckImageView;
        [self.contentView addSubview:self.checkImageView];


        UILabel *localOptionLabel=[[UILabel alloc]initWithFrame:CGRectMake(95, 0, 200, 44)];
        [localOptionLabel setBackgroundColor:[UIColor clearColor]];
        [localOptionLabel setFont:[UIFont systemFontOfSize:18.0f]];
        [localOptionLabel setTextColor:[UIColor blackColor]];

        self.optionLabel =localOptionLabel;
        [self.contentView addSubview:self.optionLabel];
        [localCheckImageView release];
        [localOptionLabel release];
    }
    return self;
}

cellfor行代码

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Question *question=[[Question alloc]init];
    question=[self.questionsArray objectAtIndex:self.currentQuestionIndex];
    //UITableViewCell *cell=[[UITableViewCell alloc]init];
    NSInteger rows=[indexPath row];
    static NSString *rowZeroCellIdentifier = @"rowZero";
    static NSString *rowOneCellIdentifier=@"rowOne";
    static NSString *rowRemainingCellIdentifier=@"rowRemaining";

    if(rows==0)
    {
    QuesNumberCell *cell =(QuesNumberCell *)[tableView dequeueReusableCellWithIdentifier:rowZeroCellIdentifier];
    if (cell == nil)
    {
        cell = [[QuesNumberCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:rowZeroCellIdentifier];

                                         }

    cell.questionNumberLabel.text = [NSString stringWithFormat:@"Question: %i",question.questionNumber];
return cell;

    }
   else if(rows==1)
    {
        QuestionTextCell *cell=(QuestionTextCell *)[tableView dequeueReusableCellWithIdentifier:rowOneCellIdentifier];
        if (cell == nil)
        {
            cell = [[QuestionTextCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:rowOneCellIdentifier];

        }

        if(_explainButtonFlag==1)
        {
         cell.questionTextView.text = [NSString stringWithFormat:@"%@",question.questionText];
        }
        else
        {
           cell.questionTextView.text = [NSString stringWithFormat:@"%@",question.explanationText];
        }

        return cell;
          }
   else{
       OptionsCell *cell=(OptionsCell *)[tableView dequeueReusableCellWithIdentifier:rowRemainingCellIdentifier];
       if (cell == nil)
       {
           cell = [[OptionsCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:rowRemainingCellIdentifier];

       }
       if(rows==2)
       {
       cell.optionLabel.text = [NSString stringWithFormat:@"%@",question.optionOneText];
       }
       if(rows==3)
       {
           cell.optionLabel.text = [NSString stringWithFormat:@"%@",question.optionTwoText];
       }
           if(rows==4)
           {
               cell.optionLabel.text = [NSString stringWithFormat:@"%@",question.optionThreeText];
           }

       //[cell setSelected:NO animated:NO];
       [cell.checkImageView setTag:indexPath.row];
       [cell .optionLabel setTag:indexPath.row+100];
       return cell;
   }
4

1 回答 1

3

它没有给出黑色,因为它正在重用单元格。在 cellForRow 方法中给文本颜色。看你给外面的颜色。

          if (cell == nil)
          {
         cell = [[QuesNumberCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:rowZeroCellIdentifier];

          }
           [cell.localOptionLabel setTextColor:[UIColor blackColor]];

在自定义单元格中,您应该只创建视图。单元格内容的数据必须在 cellForRow 方法中给出。希望这会帮助你。

于 2013-03-04T12:31:08.710 回答