uibutton背景图像不改变目标c

时间:2016-09-03 06:58:03

标签: ios objective-c uibutton

我有自定义单元格类的表视图。

内部单元格我有五个uibutton,在其自定义类中,我为所有这些都设置了出口,并且只有一个功能来处理这样的点击事件。

- (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;
    ...

}
}

通过使用标签属性我正在检查哪个按钮被点击,并且它正常工作,因为我已经使用断点检查它。当我按第一个按钮时,我想更改第一个按钮的图像。在第二个按钮我想改变第一个和第二个按钮的图像。它就像一个评级控件。我想用明星的高亮图像来改变星形图像。

但是代码不起作用。我也试过设置setNeedLayout。但是形象并没有改变。

修改

这是我的行功能单元格。

-(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;
}

这是我在QuestionCellClass中的customizeCellWithQuestions函数。

-(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];
}

1 个答案:

答案 0 :(得分:1)

您需要单独为一个状态设置Image。

使用OR运算符无效

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