如何在点击特定单元格时更改按钮?

时间:2012-10-31 06:31:25

标签: iphone ios ipad

我在我的应用程序中有一个分组表视图,有两个部分。第一行包含一行,第二行包含5.我已经向单元格添加了按钮,如下所示,`

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

`但是在某个时刻瓶颈。任何身体都指向我的怀疑方向?

2 个答案:

答案 0 :(得分:3)

如果您想单独更改按钮的图像,请在选择器中添加此项。

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

答案 1 :(得分:2)

你应该像这样编写按钮的动作方法

-(void)cellBtnAction:(id)sender {

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

试试这个我希望它会帮助你