2012-10-31 42 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]; 
} 

試試這個我希望這將有助於你

相關問題