1

我的应用程序中有一个分组表视图,有两个部分。第一个由一行组成,第二个由 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];

`但是在某些时候出现瓶颈。任何人都可以将我指向正确的方向吗?

4

2 回答 2

3

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

- (void)selector:(id)sender {
  //some code
  UIButton *button = (UIButton *)sender;
  [button setImage:[UIImage imageNamed:@"newicon.png"] forState:UIControlStateNormal];
  //some code
}
于 2012-10-31T06:36:09.020 回答
2

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

-(void)cellBtnAction:(id)sender {

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

试试这个希望对你有帮助

于 2012-10-31T06:37:29.237 回答