UITableViewに設置したUIButtonからindexpath.rowを得る
最初は、ボタンにsettagでindexpath.rowを付けていたけど、スクロールすると数字が狂うので何とかならんものかと考えていたら見つけたのでメモメモ
まず、cellForRow〜でボタンを設置する
buttonY = [UIButton buttonWithType:UIButtonTypeRoundedRect]; buttonY.frame = CGRectMake(200, 5, 50, 30); [buttonY addTarget:self action:@selector(buttonYpush:) forControlEvents:UIControlEventTouchUpInside]; [buttonY setTitle:@"Y" forState:UIControlStateNormal]; buttonY.tag = 10; [cell addSubview:buttonY];
そして、Senderで受け取ってtableViewCellに放り込む
-(void)buttonYpush:(id)sender{ NSLog(@"buttonYpush"); //ボタンでindexPathRowを取得 UIButton *btn = (UIButton *)sender; UITableViewCell *cell = (UITableViewCell *)[btn superview]; int row = [self.tableView indexPathForCell:cell].row; NSLog(@"indexpath?:%d",row); }
これでindexPath.Rowが取得できる。
以下参考
http://stackoverflow.com/questions/1802707/detecting-which-uibutton-was-pressed-in-a-uitableview
Trackbacks & Pingbacks