0

すべての 'btn1' は同じメソッドを呼び出します。

例
PS: これはUITableView

ボタンが押された行を取得する方法を知りたいです。すべてのボタンでループを実行して、最後のボタンとは異なるボタンタグを設定しようとしました:

// Handle the buttons tag
int tagCount = 100;

// mainArray is the array of the tableView
for (int i = 0; i < [self.mainArray count]; i++) {
    // I created a NSIndexPath to access the tableView cells
    NSIndexPath *nowIndex = [NSIndexPath indexPathForRow:i inSection:0];
    UITableViewCell *cell = (UITableViewCell *)[(UITableView *)self.view cellForRowAtIndexPath:nowIndex];

    // I set the button tag as the 'int';
    UIButton *shareButton = (UIButton *)[cell viewWithTag:100];
    [shareButton setTag:tagCount];

    tagCount++;
}

しかし、タグはまだ 100 です。

解決策はありますか?

4

2 に答える 2

1

UIButtonインスタンスをUIImageView、通常のイメージとオプションで強調表示されたイメージに置き換えて、それをテーブル ビュー セルの に設定できますaccessoryViewUITableViewDelegate次に、のメソッドを実装しtableView:accessoryButtonTappedForRowWithIndexPath:ます。そうすれば、タグを処理したり、ボタンへの参照を保存したりする必要がなくなります。選択状態やその他のジェスチャなどの UIButton インタラクションが必要ない場合は、このメソッドで十分です。

于 2012-08-06T02:44:04.253 に答える
1

ボタンがUIButtonインスタンスの場合は、タグ プロパティを設定します (おそらく行番号に)。イベント メソッドでは、タグ プロパティをテストして、どのボタンが押されたかを確認できます。

于 2012-08-06T00:39:38.733 に答える