0

UIButtonsの各行にありますUITableView。ボタン インデックスを検出するにはどうすればよいdidSelectRowIndexAtIndexPathですか (メソッドを使用してテーブル内の行インデックスを検出できるように)。

たとえば、UITableViewwith customがありUITableViewCellsます。10 行のうち 6 行に がありUIButtonsます。

ボタンをタップすると、ユーザーは次のビュー (詳細ビュー) に移動します。しかし、UIButton(行)インデックスを取得できません。メソッドを介さずにボタンをクリックしてユーザーdidSelectRowIndexAtIndexPathを取得しているため、行インデックスを取得できません。

どうすればそれを取得できますか?

提案。ありがとう

4

1 に答える 1

3

これを行う:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
  //other code
  // your button reference here
  // if custom cell then use cell.yourbutton
  [button addTarget:self action:@selector(buttonPressedAction:) forControlEvents:UIControlEventTouchUpInside];
  [button setTag:indexPath.row];
  //other code
}

これで、ボタンセレクターは次のようになります。

- (void)buttonPressedAction:(id)sender
{
   UIButton *button = (UIButton *)sender;
   int row = button.tag;
}
于 2012-06-20T08:18:31.057 に答える