Ravindra Bagale
Ravindra Bagale

Reputation: 17655

color of text not changing even after reload table

I have created custom classes and in that I have set all text Colour to black. after selecting row I have changed the text Colour of row contents to green. In left swipe gesture event, again I have reloaded table but the previously selected row text not changing his colour back to black, It remains green even after table reload, but its text value is changing. Why its not changing colour back to black

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

        }
    }
}

custom class code

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

Upvotes: 0

Views: 291

Answers (1)

Durgaprasad
Durgaprasad

Reputation: 1951

It is not giving black color because it is reusing cell. in cellForRow method give text color. See that you give color outside.

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

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

In custom cell you should only create view. Data to cell content must be give in cellForRow method. Hope this will help you.

Upvotes: 3

Related Questions