Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6969711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:36:58+00:00 2026-05-27T16:36:58+00:00

i have a tableView contains a couple af answers, the user will select an

  • 0

i have a tableView contains a couple af answers, the user will select an answer, if the answer is true the selected cell will be colored by green, else : wrong answer, two cells will be coloured: the selected by red color and the right by green color.

My problem is that i couldn’t change the value of indexPath by the index of val1 to locate the right cell.

here is my tableView tableView:didSelectRowAtIndexPath method :

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    NSNumber *value = [truerep objectAtIndex:0];
    NSUInteger val1 = [value integerValue];
    NSUInteger val2 = [indexPath row];

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];

    if (val1==val2) {//right answer so the color of the selected cell will be green
        cell.contentView.backgroundColor = [UIColor greenColor]; 
    }else {//wrong answer so 2 cells will be colored
        //the color of the selected cell will be red and the right cell will be green
        cell.contentView.backgroundColor = [UIColor redColor];

        // idk What to do here to change the value of indexpath by val1
    }
    [tableView deselectRowAtIndexPath:indexPath animated:YES];  
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T16:36:58+00:00Added an answer on May 27, 2026 at 4:36 pm

    Why want to do that (changing the indexPath value) ?
    This method method is called whenever the user selects a row in the table view, and [indexPath row] will give you that row index.

    The problem probably comes from the way you store the indexes of the true answers in truerep array, unable to compare direct row index with val1.

    I don’t know what truerep and [truerep objectAtIndex:0] are suppose to contain, but in your example, val1 looks like the row index for the correct answer, and it doesn’t correspond to the real right answer row index.

    Also, if you want the two cells to be colored, you will have to change a bit your code.
    Here, with the if/else, you only get one colored when the user selects a row.

    EDIT based on your comments

    You probably want to loop over all row and determine which are to be colored in red and green. Here is an example :

    -(void)tableView:(UITableView *)tableView
     didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
        NSUInteger rowIndex = [indexPath row];
        NSNumber *value = [truerep objectAtIndex:0];
        NSUInteger val1 = [value integerValue]; // index of the correct answer row
        UITableViewCell *cell;
    
        if(rowIndex = val1) { // only color the right cell in green
            cell = [tableView cellForRowAtIndexPath:ip];
            cell.contentView.backgroundColor = [UIColor greenColor];
        }
        else {
            for(rowIndex = 0; rowIndex < totalRowsCount; rowIndew += 1) {
                NSIndexPath *ip = [NSIndexPath indexPathWithIndex:rowIndex];
                cell = [tableView cellForRowAtIndexPath:ip];
                if(val1 == rowIndex) {
                    cell.contentView.backgroundColor = [UIColor greenColor];
                }
                else {
                    cell.contentView.backgroundColor = [UIColor redColor]; 
                }
            }
        }
        [tableView deselectRowAtIndexPath:indexPath animated:YES];  
    }
    

    You surely know the value for totalRowsCount..

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a tableview which contains images in cell. If I touch an image
I have a tableview which contains custom cells where each cell has some PDF
I have a tableview that contains cell images and names as text. At the
I have an array named bookmark,it contains values,i displayed it in a tableview cell,i
I have a controller that contains a tableView that spawns another controller modally to
I have an app which contains a scrollview with several tableviews. Each tableview is
I have a tableView and when the user is selecting one of the cells,
I have a .xib file for my viewController. It contains a TableView and a
I have a tableView with multiple sections. When a sections contains only one row,
I have a UITableViewController that contains a tableView. I want to set its scrollView

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.