iphone - 即使重新加载表格后文本的颜色也不会改变

标签 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代码

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

最佳答案

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

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

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

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

关于iphone - 即使重新加载表格后文本的颜色也不会改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15201417/

相关文章:

ios - 如何获取所有未决远程通知的数组?

iphone - 如何避免将一个单词拆分为两个 UITextView

ios - 我怎么知道在ios中打开或关闭飞行模式

ios - 如何让 uitableview 一次加载多个部分?

iphone - 获取自定义 UITableViewCell 的字符串属性值

iphone - 以编程方式检查用户是否登录到 Appstore?

iphone - iOS Cordova 2.0.0 应用程序中的 Google Analytic 未更新

iOS 6 运行时 header 方法使用其包标识符打开应用程序[非越狱]

ios - 如何在自动布局中更改选择器 View 的字体大小

ios - 来自动态 UITableView 的动态转场似乎是不可能的