-1

cellForRowメソッドの各行の前にイメージ ボタン ( a.png ) を作成しました。同じメソッド自体で、ボタンが押されたときに実行されるコードを持つbuttonPressedメソッドを呼び出しています。そして、ボタンを押すと、ボタンの画像が同じサイズの別の画像( b.png )に変更されます。

その方法を教えてください。

4

4 に答える 4

0

Custom Cell に UITableView と UIButton の Custom Cell を作成します。

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 に答える
0
[but setBackgroundImage:[UIImage imageNamed:@"a.png"] forState:UIControlStateNormal];
[but setBackgroundImage:[UIImage imageNamed:@"b.png"] forState:UIControlStateSelected];

これを試して......

于 2012-09-28T06:38:24.673 に答える
-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 に答える