开发者

how to change the button image inside the table cell while clicking the button in iphone

I have a tableview in my application. I created a button inside the table cell and assigned a background image to it. I wanted to change the background image when i click that button. I assigned an action for the button like this

-(void)goodBtnClicked:(id)sender {
    [goodBtn setBackgroundImage:[UIImage imageNamed:@"camera.png"] forState:UIContr开发者_高级运维olStateNormal];
}

But, the image is not getting replaced with the new image. Can any one help me with this?


You should make sure that your image is actually there.

-(void)goodBtnClicked:(id)sender {
    UIImage *img = [UIImage imageNamed:@"camera.png"];
    NSAssert(img, @"Image shouldn't be nil");
    [goodBtn setBackgroundImage:img forState:UIControlStateNormal];
}


try with

-(void)goodBtnClicked:(id)sender {
    [sender setBackgroundImage:[UIImage imageNamed:@"camera.png"] forState:UIControlStateNormal];
}


Go through all the subviews and locate your button then change background image

- (void)buttonPressed:(UIButton *)sender{

    UITableViewCell *cell = (UITableViewCell *)sender.superview;
    for (UIView *sub in cell.subviews) {

        if([sub isMemberOfClass:[UIButton class]])
        {
          UIButton * button=[[UIButton alloc] init];
          button=sub;
          [button setImage:[UIImage imageNamed:@"image.png"] forState:UIControlStateNormal];
        }

    }

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜