cellForRowメソッドの各行の前にイメージ ボタン ( a.png ) を作成しました。同じメソッド自体で、ボタンが押されたときに実行されるコードを持つbuttonPressedメソッドを呼び出しています。そして、ボタンを押すと、ボタンの画像が同じサイズの別の画像( b.png )に変更されます。
その方法を教えてください。
cellForRowメソッドの各行の前にイメージ ボタン ( a.png ) を作成しました。同じメソッド自体で、ボタンが押されたときに実行されるコードを持つbuttonPressedメソッドを呼び出しています。そして、ボタンを押すと、ボタンの画像が同じサイズの別の画像( b.png )に変更されます。
その方法を教えてください。
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];
次の方法を試してください
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];
}
}
[but setBackgroundImage:[UIImage imageNamed:@"a.png"] forState:UIControlStateNormal];
[but setBackgroundImage:[UIImage imageNamed:@"b.png"] forState:UIControlStateSelected];
これを試して......
ボタンを初期化するときにこれを試してください
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
}