0

カスタムUITableViewCellを作成したUITableViewがあります。テーブルビューの各行にはボタンがあります。ボタンをクリックしたときにセクション番号を知りたいので、どのセクションボタンがクリックされたかがわかります。スタックで見つかったいくつかのことをすでに試しましたが、何も機能していません。

UIButton *b = sender; 
NSIndexPath *path = [NSIndexPath indexPathForRow:b.tag inSection:0]; 
NSLog(@"Row %d - Section : %d", path.row, path.section);
4

4 に答える 4

5

あなたが何を試したのかわかりませんが、私はこのようなことをするかもしれません。ここで、メモリからいくつかの擬似コードを実行します。

- (void)buttonClicked:(id)sender {
    CGPoint buttonOrigin = [sender frame].origin;
    // this converts the coordinate system of the origin from the button's superview to the table view's coordinate system.
    CGPoint originInTableView = [self.tableView convertPoint:buttonOrigin fromView:[sender superview];

    // gets the row corresponding to the converted point
    NSIndexPath rowIndexPath = [self.tableView indexPathForRowAtPoint:originInTableView];

    NSInteger section = [rowIndexPath section];

}

UITableView明確に考えている場合、これにより、ボタンがセル内に直接ない場合に柔軟性が得られます。たとえば、中間ビュー内にネストしたとします。

残念ながら、NSTableView に相当する iOS はないようです。rowForView:

于 2013-05-18T06:12:29.993 に答える
3

ボタンクリックのハンドラーを作成し、tableView:cellForRowAtIndexPath:メソッドに追加します

- (void)buttonPressed:(UIButton *)button{

    UITableViewCell *cell = button.superView.superView;

    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    //Now you have indexPath of the cell 
    //do your stuff here

}
于 2013-05-18T06:01:22.733 に答える
0

カスタム UITableViewCell を作成するときはcellForRowAtIndexPath、そのセクションをパラメーターとして渡す必要があります。次のようになります。

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
    MyCustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyIdentifier"];

  if (!cell)
  { 
  cell = [[[MyCustomCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MyIdentifier" section:indexPath.section] autorelease];
  }

    return cell;
 }

MyCustomCellこれで、セルのセクションが認識され、クラスでクリック メソッドを実行するときに使用できます。

于 2013-05-18T06:01:21.317 に答える
0

これを試して、

最初にセクションをタグとしてボタンに割り当て、メソッドのボタンにターゲットを追加しますcellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ...
    [cell.btnSample setTag:indexPath.section];
    [cell.btnSample addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    ...
}

定義した IBAction の送信者からセクションをタグとして取得します ( buttonClicked here )。

-(IBAction)buttonClicked:(id)sender
{
    NSLog(@"Section: %d",[sender tag]);
}
于 2013-05-18T06:03:59.973 に答える