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

  • BrianSlickBrianSlick Treadmill Desk Ninja Posts: 10,690Tutorial Authors, Registered Users @ @ @ @ @ @ @ @
    edited September 2015
    Reload the table.

    Edit: Misunderstood the question.

    Grab the indexPath for the correct answer, then grab the appropriate cell for that indexPath, and do whatever needs to be done.
    Professional iOS App Development. Available for hire.
    BriTer Ideas LLC - WWW | Facebook | Twitter | LinkedIn

    BTIKit | SlickShopper 2 | Leave a PayPal donation
  • RickSDKRickSDK Posts: 1,240Registered Users @ @ @ @
    but also reloadData. The code for setting the colors needs to be in cellForRowAtIndexPath.
  • Duncan CDuncan C Posts: 9,114Tutorial Authors, Registered Users @ @ @ @ @ @ @
    RickSDK wrote: »
    but also reloadData. The code for setting the colors needs to be in cellForRowAtIndexPath.

    You could do it either way. If the cell is on-screen (and it sound like in this use-case there are a fixed number of cells that are always all visible) then you can ask the table view for a cell and make changes to it. That's not a common way of doing it, but it it would work in this situation.)
    Regards,
    Duncan C
    WareTo

    widehead.gif
    Animated GIF created with Face Dancer, available for free in the app store.

    I'm available for one-on-one help at CodeMentor
Sign In or Register to comment.