Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

How do I change all of the UITableViewCell color on one selection?

naak2803naak2803 FrankfurtPosts: 24New Users @
Hello.
I'm developing a quiz engine. Now I'm stuck on this issue.
I have a UITableview with 4 rows (answers). What I want to do is that when the user presses on a row that has incorrect it turns red. So far so good. The problem begins now... I also want to change the correct answer row to green, how do I accomplish that?

this is my code so far... this code only turns the correct answer to green, and the incorrect answer to red. What I want is when incorrect turns red at the same time the correct answer should turn green.
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    Question *question = [self.question objectAtIndex:self.iCurrentQuestion];

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    UILabel *label = (UILabel *)[cell viewWithTag:100];
    
    NSString *chosenAnswer = label.text;
    
    if ([chosenAnswer isEqualToString:question.correctAnswer])
    {
        self.CGFCorrectAnsweredQuestions++;
        
        UIView *bgColorView = [[UIView alloc] init];
        bgColorView.backgroundColor = [UIColor colorWithRed:0.525 green:0.816 blue:0.373 alpha:1];
        [cell setSelectedBackgroundView:bgColorView];
        
        label.textColor = [UIColor whiteColor];
    }
    else
    {
        UIView *bgColorView = [[UIView alloc] init];
        bgColorView.backgroundColor = [UIColor colorWithRed:0.949 green:0.29 blue:0.329 alpha:1];
        [cell setSelectedBackgroundView:bgColorView];
        
        label.textColor = [UIColor whiteColor];
        
        self.lblExplaination.text = question.explaination;
    }
    
    self.tableView.userInteractionEnabled = NO;
    
    [UIView animateWithDuration:1.0f
                          delay:0.0
                        options:UIViewAnimationOptionCurveEaseInOut
                     animations:^{self.viewNextQuestion.hidden = NO;}
                     completion:nil];
    
    if(self.iCurrentQuestion+1 == self.question.count)
        [self.btnNextQuestion setTitle:@"GIVE UP" forState: UIControlStateNormal];
}

Replies

Sign In or Register to comment.