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

  • Home
  • SEARCH
  • 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 4559706
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T17:49:36+00:00 2026-05-21T17:49:36+00:00

I have a tableView showing some controls.Like options for the correct ans in UIButton.I

  • 0

I have a tableView showing some controls.Like options for the correct ans in UIButton.I want to change the buttons background image in the correct ans and the wrong ans in the click event.how can i do this?Can any one give me a idea?
i am trying to change the background image as :

 [cell.ansBtn2 setImage:[UIImage imageNamed:@"option_red.png"]  forState:UIControlStateSelected];

and i have set the backgound as :

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

     static NSString *CellIdentifier = @"BeginingCell";

     cell=(BeginingCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];


    if (cell == nil) {

               NSArray *topLevelObjects=[[NSBundle mainBundle] loadNibNamed:@"BeginingCell" owner:self options:nil ];

        for(id CurrentObject in topLevelObjects)
        {
            if ([CurrentObject isKindOfClass:[BeginingCell class]]) {

                cell=(BeginingCell *) CurrentObject;
                cell.selectionStyle = UITableViewCellSelectionStyleNone;


                break;
            }
        }

    }


    if(indexPath.row==0)
    {


                imageURL=[dictionary objectForKey:@"questionImage"];

        NSURL *url = [NSURL URLWithString:imageURL];
        NSData *data = [NSData dataWithContentsOfURL:(NSURL *)url];
        UIImage *img = [[UIImage alloc]  initWithData:data];



        cell.lblScoreCurrent.text=[NSString stringWithFormat:@"%d", reloader];

        cell.lblScore.text=@"/30";
        cell.myImageView.image = img;
        cell.SectionTitle.text=[dictionary objectForKey:@"question"];




        cell.ansBtn1.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
        cell.ansBtn2.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
        cell.ansBtn3.titleLabel.lineBreakMode = UILineBreakModeWordWrap;
        cell.ansBtn4.titleLabel.lineBreakMode = UILineBreakModeWordWrap;



        cell.ansBtn1.titleLabel.textAlignment = UITextAlignmentCenter;
        cell.ansBtn2.titleLabel.textAlignment = UITextAlignmentCenter;
        cell.ansBtn3.titleLabel.textAlignment = UITextAlignmentCenter;
        cell.ansBtn4.titleLabel.textAlignment = UITextAlignmentCenter;


        [cell.ansBtn1 setTitle:[dictionary objectForKey:@"option1"] forState:UIControlStateNormal];
        [cell.ansBtn2 setTitle:[dictionary objectForKey:@"option2"] forState:UIControlStateNormal];
        [cell.ansBtn3 setTitle:[dictionary objectForKey:@"option3"] forState:UIControlStateNormal];
        [cell.ansBtn4 setTitle:[dictionary objectForKey:@"option4"] forState:UIControlStateNormal];


        [cell.ansBtn1 addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        [cell.ansBtn2 addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        [cell.ansBtn3 addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        [cell.ansBtn4 addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];



    }


    return cell;

}

And here is the click Event :

      -(void) btnAction:(id) sender {

NSString *str =((UIButton*)sender).titleLabel.text;

NSLog(@"%@",str);
NSLog(@"%@",[dictionary objectForKey:@"option3"]);

correctAns=[dictionary objectForKey:@"answer"];

if(str==[dictionary objectForKey:@"option1"])
{
    selectedAns=@"1";

    if ([selectedAns compare:correctAns]==NSOrderedSame) 
    {

    [cell.ansBtn1 setBackgroundImage:[UIImage imageNamed:@"option_green.png"] forState:UIControlStateHighlighted];
     NSLog(@"this is correct");

     countCorrect++;
     cell.lblRating.text=[NSString stringWithFormat:@"%i", countCorrect];


    }

    else 
    {

        [cell.ansBtn1 setBackgroundImage:[UIImage imageNamed:@"option_red.png"] forState:UIControlStateHighlighted];
        cell.lblRating.text=[NSString stringWithFormat:@"%i", countCorrect];

    }

}

else if(str==[dictionary objectForKey:@"option2"])
{
    selectedAns=@"2";
    if ([selectedAns compare:correctAns]==NSOrderedSame) 
    {
        [cell.ansBtn2 setBackgroundImage:[UIImage imageNamed:@"option_green.png"] forState:UIControlStateHighlighted];
        NSLog(@"this is correct");
        countCorrect++;
        cell.lblRating.text=[NSString stringWithFormat:@"%i", countCorrect];

    }

    else 
    {
        [cell.ansBtn2 setBackgroundImage:[UIImage imageNamed:@"option_red.png"] forState:UIControlStateHighlighted];
        cell.lblRating.text=[NSString stringWithFormat:@"%i", countCorrect];

    }
}


else if(str==[dictionary objectForKey:@"option3"])
{

    selectedAns=@"3";
    NSLog(@"selected ans is %@",selectedAns);
    NSLog(@"correct ans is %@",correctAns);

    if ([selectedAns compare:correctAns]==NSOrderedSame) 
    {
        [cell.ansBtn3 setBackgroundImage:[UIImage imageNamed:@"option_green.png"] forState:UIControlStateHighlighted];
        NSLog(@"this is correct");

        countCorrect++;
        cell.lblRating.text=[NSString stringWithFormat:@"%i", countCorrect];

    }

    else 
    {
        [cell.ansBtn3 setBackgroundImage:[UIImage imageNamed:@"option_red.png"] forState:UIControlStateHighlighted];
        cell.lblRating.text=[NSString stringWithFormat:@"%i", countCorrect];

    }

}

else if(str==[dictionary objectForKey:@"option4"])
{

    selectedAns=@"4";
    NSLog(@"selected ans is %@",selectedAns);
    NSLog(@"correct ans is %@",correctAns);

    if ([selectedAns compare:correctAns]==NSOrderedSame) 
    {

        [cell.ansBtn4 setBackgroundImage:[UIImage imageNamed:@"option_green.png"] forState:UIControlStateHighlighted];

        countCorrect++;
        cell.lblRating.text=[NSString stringWithFormat:@"%i", countCorrect];

    }

    else 
    {
        [cell.ansBtn4 setBackgroundImage:[UIImage imageNamed:@"option_red.png"] forState:UIControlStateHighlighted];
        cell.lblRating.text=[NSString stringWithFormat:@"%i", countCorrect];            

    }

}


else {
    NSLog(@"ERROR");
}


reloader++;

if (reloader>=[QuestionMutableArray count]) {



    NSString *str=[self GetSkill];
    NSString *totalTime=[NSString stringWithFormat:@"%i . %i minutes",7-timeMin,60-timeSecond];
    NSString *str2=[NSString stringWithFormat:@"Game Over :\n Knowlwdge Level : %@  \n Total Correct Answer :%i \n Total Time : %@ \n Total Question Answered : %i \n",str,countCorrect,totalTime,reloader];

    appAlert=[[UIAlertView alloc]initWithTitle:@"Game Over :\n" message:str2 
                                               delegate:self cancelButtonTitle:@"Exit" otherButtonTitles:nil];
    [appAlert addButtonWithTitle:@"PlayAgain"];



    UIImage *alertBoxImage = [UIImage imageNamed:@"first_look.jpg"];

    UIImageView *_backgroundImageView = [[UIImageView alloc] initWithImage:alertBoxImage];

    _backgroundImageView.frame = CGRectMake(0, 0, 282, 220);

    _backgroundImageView.contentMode = UIViewContentModeScaleToFill;

    [appAlert addSubview:_backgroundImageView];

    [appAlert sendSubviewToBack:_backgroundImageView]; 


    [appAlert show];        
    [dictionary release];
    [QuestionMutableArray release];
    countCorrect=0;


}
else {

    dictionary=[QuestionMutableArray objectAtIndex:reloader];
    [self.tableView reloadData];
}

}

problem is when i am clicking a button for the first time the background is not changing.but in the next click i am getting the change according to the change logic set(i mean green/red change)…what should i do in this case.What is the solution?

  • 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-21T17:49:37+00:00Added an answer on May 21, 2026 at 5:49 pm

    More Edit:
    Don’t use == to check if the strings are equal or not, instead use isEqualToString: or compare: function

    http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html

    you can do

    if([string1 compare:string2] == NSOrderedSame)
    {
        //Do something...
    }
    

    because == will check 2 pointers address in memory location and not the contents of those pointers.

    OLD

    You can use

    http://developer.apple.com/library/ios/#documentation/uikit/reference/UIButton_Class/UIButton/UIButton.html#//apple_ref/occ/instm/UIButton/setBackgroundImage:forState:

    or

    http://developer.apple.com/library/ios/#documentation/uikit/reference/UIButton_Class/UIButton/UIButton.html#//apple_ref/occ/instm/UIButton/setImage:forState:

    If you are showing ans as title label of button, then you have to use setBackgroundImage:forState:

    Edit
    instead of

     [cell.ansBtn2 setImage:[UIImage imageNamed:@"option_red.png"]  forState:UIControlStateSelected];
    

    you should use

     [cell.ansBtn2 setBackgroundImage:[UIImage imageNamed:@"option_red.png"]  forState:UIControlStateSelected];
    

    cause you also have to show your ans as title label of that button.

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

Sidebar

Related Questions

Here's my case: I have a table view showing contacts. Add button in the
I have a grouped table view with textfields in the tableview. For the keyboard
I have some VERY simple code to return the title for a section header:
I have a table view that's showing a list of bank transactions. When I
Do you know how to have some cells appear in a table view after
In my iPhone application I have a table view. When user taps any row,
I have built a simple Eclipse plugin where a user may use a TableViewer
Have just started using Google Chrome , and noticed in parts of our site,
Have you ever seen any of there error messages? -- SQL Server 2000 Could
Have you guys had any experiences (positive or negative) by placing your source code/solution

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.