1

カスタムのuitableviewcellがあります。uibutton、アクション付きのuibuttonがあります。では、uibuttonが押された行のindexPathをどのように知ることができますか?

ターゲット:各行のuibuttonsには画像があります。したがって、ユーザーがボタンをタップすると、アラートビューが表示され、答えが「はい」の場合、uibuttonの画像が変化します。

4

3 に答える 3

3

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];
 }
于 2012-09-04T18:01:10.833 に答える
0

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>

于 2012-09-04T15:55:21.183 に答える
-1

現在のセルの行をUIButtonのタグとして割り当てることができます。ターゲット/アクションで、ボタンのタグをチェックして、ボタンが押された行を見つけることができます。

于 2012-09-04T15:50:54.517 に答える