Yawar Yawar - 3 months ago 10
Objective-C Question

uibutton background image not changing objective c

I have table view with custom cell class.

Inside cell I've five uibuttons and in its custom class I've made outlets for all of them and have only one function to handle their tap events like this.

- (IBAction)starClicked:(id)sender {
UIButton *btn = (UIButton*)sender;

switch (btn.tag){
case 501:
self.starOne = [UIButton buttonWithType:UIButtonTypeCustom];
[self.starOne setBackgroundImage:[UIImage imageNamed:@"day-time-temp-icon.png"] forState:UIControlStateNormal|UIControlStateHighlighted|UIControlStateSelected];
[self.starOne setTitle:@"23" forState:UIControlStateNormal];
[self.starOne setNeedsLayout];
self.starTwo = [UIButton buttonWithType:UIButtonTypeCustom];
[self.starTwo setBackgroundImage:[UIImage imageNamed:@"opening-time-icon.png"] forState:UIControlStateNormal|UIControlStateHighlighted|UIControlStateSelected];
self.starThree = [UIButton buttonWithType:UIButtonTypeCustom];
[self.starThree setBackgroundImage:[UIImage imageNamed:@"opening-time-icon.png"] forState:UIControlStateNormal|UIControlStateHighlighted|UIControlStateSelected];
self.starFour = [UIButton buttonWithType:UIButtonTypeCustom];
[self.starFour setBackgroundImage:[UIImage imageNamed:@"opening-time-icon.png"] forState:UIControlStateNormal|UIControlStateHighlighted|UIControlStateSelected];
self.starFive = [UIButton buttonWithType:UIButtonTypeCustom];
[self.starFive setBackgroundImage:[UIImage imageNamed:@"opening-time-icon.png"] forState:UIControlStateNormal|UIControlStateHighlighted|UIControlStateSelected];
break;
...

}
}


by using tag property I am checking which button is tapped, and it is working fine as I have checked it using break point. and when I press first button I want to change image of first button. And on second button I want to change image of first and second button. It is like a rating control. I want to change star image with highlighted image of star.

but below code not working. I have tried setting setNeedLayout too. But image is not changing.

Edit

this is my cell for row function.

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
QuestionsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"questCell"];
if(cell == nil){
cell = [[QuestionsCell alloc] init];
}
[cell customizeCellWithQuestions:[self.questionList objectAtIndex:indexPath.row] textSize:1 index:indexPath.row];
return cell;
}


This is my
customizeCellWithQuestions
function inside my QuestionCellClass.

-(void)customizeCellWithQuestions:(Question *)question textSize:(float)textSize index:(NSUInteger)index{
[self.questionTitleTv setText:[question.question capitalizedString]];
[self.questionIndexLbl setText:[NSString stringWithFormat:@"%lu",(unsigned long)index+1]];
self.backgroundColor = [UIColor clearColor];
self.contentView.backgroundColor = [UIColor colorWithRed: 213.0/255.0 green: 213.0/255.0 blue: 213.0/255.0 alpha: 0.0];
}

Answer

You need to set Image for one state individually.

Using OR operator won't work

    [self.starOne setBackgroundImage:[UIImage imageNamed:@"day-time-temp-icon.png"] forState:UIControlStateNormal];