カスタムのuitableviewcellがあります。uibutton、アクション付きのuibuttonがあります。では、uibuttonが押された行のindexPathをどのように知ることができますか?
ターゲット:各行のuibuttonsには画像があります。したがって、ユーザーがボタンをタップすると、アラートビューが表示され、答えが「はい」の場合、uibuttonの画像が変化します。
カスタムのuitableviewcellがあります。uibutton、アクション付きのuibuttonがあります。では、uibuttonが押された行のindexPathをどのように知ることができますか?
ターゲット:各行のuibuttonsには画像があります。したがって、ユーザーがボタンをタップすると、アラートビューが表示され、答えが「はい」の場合、uibuttonの画像が変化します。
Try below code snippet:
In cellForRowAtIndexPath method. set indexpath.row as button tag
[cellButton addTarget:self action:@selector(cellBottonClicked:) forControlEvents:UIControlEventTouchUpInside];
cellButton.tag = indexPath.row;
-(IBAction) cellBottonClicked:(id)sender{
NSInteger row = [sender tag];
//you can get indexpath from row
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:0];
CustomTableViewCell *cell = (CustomTableViewCell *)[self.tableView cellForRowAtIndexPath:indexPath];
// from this cell you can fetch and change image of selected cell's button.
}
[EDIT : Change Only image of Button]
If you only required to change Image of Button on click event.then I think, you do not need for indexpath or cell.
try this code. Hope it will helps:
-(IBAction) cellBottonClicked:(id)sender{
UIButton *btn = (UIButton *)sender;
[btn setImage:yourImage forState:UIControlStateNormal];
}
You should also be able to get the indexPath by:
NSIndexPath *indexPathForButton = [self.tableView indexPathForSelectedRow];
or in your .h file, add:
@property (nonatomic, retain) NSIndexPath* indexPathForButton;
and change the indexPath, whenever it's used. (self.indexPathForButton = indexPath;)
But using tags is much comfortable…</p>
現在のセルの行をUIButtonのタグとして割り当てることができます。ターゲット/アクションで、ボタンのタグをチェックして、ボタンが押された行を見つけることができます。