2012-10-31 102 views
1

我在我的申请分组的表视图具有两个sections.First一个由一个行的,而第二个与5.I已经添加按钮的细胞,这样,`如何在点击某个特定单元格时更改该按钮?

UIButton *newBtn=[UIButton buttonWithType:UIButtonTypeCustom]; 
    [newBtn setFrame:CGRectMake(5,10,35,30)]; 
    [newBtn setImage:[UIImage imageNamed:@"icon.png"] forState:UIControlStateNormal]; 
    [newBtn addTarget:self action:@selector(selector:)  forControlEvents:UIControlEventTouchUpInside]; 
    newBtn.tag=4; 
    [cell.contentView addSubview:newBtn]; 

Now in that selector methode i need to change it with another image.I am trying to do that in this way

UITableViewCell *cell=(UITableViewCell *)[sender superview]; 

    NSIndexPath *path=[self.mtableview indexPathForCell:cell]; 

`可是瓶颈一些point.Can任何机构点我在赖特的方向吗?

+0

什么是你的行动方法按钮? –

+0

检查我的答案。 – iDev

回答

3

如果您想单独更改按钮图像,请将其添加到您的选择器中。

- (void)selector:(id)sender { 
    //some code 
    UIButton *button = (UIButton *)sender; 
    [button setImage:[UIImage imageNamed:@"newicon.png"] forState:UIControlStateNormal]; 
    //some code 
} 
+0

我只需要在相应的单元格中更改按钮。其他人需要相同 –

+1

是的,这样做。你在不同的单元格中有不同的按钮吗?尝试一下,让我知道。或者你的意思是说,如果你点击单元格中的任何其他按钮,即使它们是不同的按钮,它们也不应该改变? – iDev

+0

No..all r same button.that icon.png.when点击它后,点击按钮的背景图像需要更改 –

2

你应该写你的操作方法按钮类似于这样

-(void)cellBtnAction:(id)sender { 

    UIButton *btn=(UIButton *)sender; 
    //Write code for change image to button 
    [btn setImage:imgObj forState:UIControlStateNormal]; 
} 

试试这个我希望这将有助于你

相关问题