-1

我在cellForRow方法的每一行前面创建了一个图像按钮 ( a.png )。在同一方法本身中,我正在调用buttonPressed方法,该方法具有在按下按钮时要执行的代码。我希望在按下按钮时,按钮图像应该更改为另一个相同大小的图像(b.png)。

请帮助如何做到这一点。

4

4 回答 4

0
[but setBackgroundImage:[UIImage imageNamed:@"a.png"] forState:UIControlStateNormal];
[but setBackgroundImage:[UIImage imageNamed:@"b.png"] forState:UIControlStateSelected];

尝试这个......

于 2012-09-28T06:38:24.673 回答
0

为自定义单元格上的 UITableView 和 UIButton 创建自定义单元格。

为 UIButtion 创建属性。

在 cellForRowAtIndexPath: 方法中

//Set Action on  Button  in Table View Row

[cell.UIButtonOutlet addTarget:self action:@selector(checkButtonTapped:event:)  forControlEvents:UIControlEventTouchUpInside]; 

这将从您的按钮创建操作。

在方法中

-(void)checkButtonTapped:(id)sender event:(id)event
{

NSSet *touches=[event allTouches];

  UITouch *touch=[touches anyObject];

 CGPoint currentTouchPosition = [touch locationInView:self.historyTableView];

//GET IndexPath

NSIndexPath *indexPath=[self.TableView indexPathForRowAtPoint: currentTouchPosition];        

 selectedCell= [PairTableView cellForRowAtIndexPath:indexPath];

[selectedCell.UIButtonOutlet setImage:[UIImage imageNamed:@"a.png"] forState:UIControlStateNormal];
于 2012-09-28T06:56:24.997 回答
0

试试下面的方法

 UIButton *button = [UIButton buttonWithType:UIButtonTypeContactAdd];
        [button setFrame:CGRectMake(12,12,200,200)];
        [button addTarget:self action:@selector(click: withEvent:) forControlEvents:UIControlEventTouchUpInside];
        [button setTag:indexPath.row];
        [button setBackgroundImage:[UIImage imageNamed:@"a.png"] forState:UIControlStateNormal];

        [cell.contentView addSubview:button];

然后在按钮的点击动作方法中

    -(void)click:(UIButton *)button :withEvent:(UIEvent *)events{
       [self yourFunction:(UIButton *)button];

    }

然后最后

   -(void)yourFunction:(UIButton *)button{
     button.selected = ! button.selected;

                if (button.selected) {
                    [button setBackgroundImage:[UIImage imageNamed:@"a.png"] forState:UIControlStateNormal];
                }
                else{
                    [button setBackgroundImage:[UIImage imageNamed:@"b.png"] forState:UIControlStateNormal];

                }

}
于 2012-09-28T06:57:41.317 回答
-1

初始化按钮时试试这个

UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:@"a.png"] forState:UIControlStateNormal];

然后像这样修改你的方法(按下按钮时调用)

-(void)yourMethod:(id)sender{

    UIButton *btn=(UIButton*)sender;
    [btn setImage:[UIImage imageNamed:@"b.png"] forState:UIControlStateNormal];

    //your other code

}
于 2012-09-28T06:37:54.810 回答