У меня есть tableView, показывающий некоторые элементы управления. Подобные опции для правильных ответов в UIButton. Я хочу изменить фоновое изображение кнопок в правильных ответах и неправильных ответах в событии щелчка. Как я могу это сделать?дайте мне идею?я пытаюсь изменить фоновое изображение как:
[cell.ansBtn2 setImage:[UIImage imageNamed:@"option_red.png"] forState:UIControlStateSelected];
, и я установил фоновое изображение как:
- (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;
}
И вот событие клика:
-(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];
}
}
проблема в том, что когда я нажимаю кнопку в первый раз, фон не меняется. Но при следующем щелчке я получаю изменение в соответствии с набором логики изменений (я имею в виду зеленое / красное изменение) ... что я долженделать в этом случае. Какое решение?