在每个单元格的UITableView中显示新图像而不是另一个图像按钮

时间:2012-09-28 13:57:41

标签: iphone objective-c xcode uitableview uibutton

我想在单击该按钮后立即在表视图控制器中的另一个图像按钮顶部添加图像。我在.h文件中有UIButton *按钮。

    CellForRow:{
    button=[[UIButton alloc]initWithFrame:CGRectMake(230,0,40,40); 
   [button addTarget:self action:@selector(buttonPressed:withEvent:)                 forControlEvents:UIControlEventTouchUpInside]; 
[button setImage:[UIImage imageNamed:"a"] forState:UIControlStateNormal];
button.tag=indexPath.row;
[cell addSuBView:button];
[cell setIndentationWidth:1];
[cell setIndendationLevel:1];
}
-(void)buttonPressed:(id) sender withEvent: (UIEvent *) event{
UIButton *mybtn=(UIButton*) sender;
[mybtn setImage:[UIImage imageNamed:"b"] forState:UIControlStateNormal];
}

这样做会在第一个图像的顶部显示第二个图像。但我想要的是取代旧图像。

1 个答案:

答案 0 :(得分:0)

-(void)buttonPressed:(id)sender withEvent:(UIEvent *)event{
UIButton *button = (UIButton *)sender;
button.selected = ! button.selected;

                if (button.selected) {
                    [button setBackgroundImage:[UIImage imageNamed:@"a.png"] forState:UIControlStateNormal];
                }
                else{
                    [button setBackgroundImage:[UIImage imageNamed:@"b.png"] forState:UIControlStateNormal];

                }

}