重载表后文本颜色不变

时间:2013-03-04 12:18:09

标签: iphone ios uitableview

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

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

cellforRow code

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

1 个答案:

答案 0 :(得分:3)

它没有给出黑色,因为它正在重复使用细胞。 在cellForRow方法中给出文本颜色。 看到你在外面给出了颜色。

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

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

在自定义单元格中,您应该只创建视图。必须在cellForRow方法中提供数据到单元格内容。 希望这会对你有所帮助。