私はそれに2つのアクションを持つテーブルビューを持っています。私はデリゲートのdidSelectRowAtIndexPathを使用しています。もう1つは、セルのボタンで何か他の操作をしたいのです。私の問題は、私がindexpathを取得するのに成功しないということです?それを行うベストプラクティスは何ですか?セル内のボタンアクションからテーブルビュー内のindexPathを取得する方法は?
答えて
あなたは
[cell.contentView addSubview:button];
、としてセルにボタンを追加した場合、その後、あなたはインデックスパスをとして取得することができ、ここで
- (void)onButtonTapped:(UIButton *)button {
UITableViewCell *cell = (UITableViewCell *)button.superview.superview;
NSIndexPath *indexPath = [tableView indexPathForCell:cell];
// Go ahead
}
ありがとう! UIButton * button =(UIButton *)送信者。 UITableViewCell * cell =(UITableViewCell *)button.superview.superview; –
よろしく!ようこそ! – EmptyStack
スーパービューからtableViewにアクセスすべきではありません。これは動作しますが、新しいiOSでは動作しない可能性があります。 6/7 Appleからの古典的な例は別のスーパービューを追加しました。これはクラッシュするでしょう – CW0007007
は、カスタムボタンの完全なコードです:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
CustomCellFilter *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (!cell) {cell = [[CustomCellFilter alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];}
//configure your cell here
cell.titleLabel.text = @"some title";
//add button
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
resetButton.frame = CGRectMake(40, 10, 18, 18);
[resetButton addTarget:self action:@selector(onButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[resetButton setImage:[UIImage imageNamed:@"someimage.png"] forState:UIControlStateNormal];
[cell.contentView addSubview:myButton];
//afterwards return the cell
return cell;
- (void)onButtonTapped:(UIButton *)button {
UITableViewCell *cell = (UITableViewCell *)button.superview.superview;
NSIndexPath *indexPath = [filterTable indexPathForCell:cell];
NSLog(@"%@", indexPath);
//use indexPath here...
}
ボタンが移動された場合、またはAppleがアクセサリビューのビュー階層を変更した場合、このメソッドはチェーンを上にして表示されますUITableViewCellのために:
- (void)tapAccessoryButton:(UIButton *)sender {
UIView *parentView = sender.superview;
// the loop should take care of any changes in the view heirarchy, whether from
// changes we make or apple makes.
while (![parentView.class isSubclassOfClass:UITableViewCell.class])
parentView = parentView.superview;
if ([parentView.class isSubclassOfClass:UITableViewCell.class]) {
UITableViewCell *cell = (UITableViewCell *) parentView;
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
[self tableView:self.tableView accessoryButtonTappedForRowWithIndexPath:indexPath];
}
}
私は、このソリューションを使用しています - あなたはコレクションビューを使用している場合はポイント
CGPoint center= [sender center];
CGPoint rootViewPoint = [[sender superview] convertPoint:center toView:_tableView1];
NSIndexPath *indexPath = [_tableView1 indexPathForRowAtPoint:rootViewPoint];
NSLog(@"%@",indexPath);
その作業完璧
これは 'superView.superView'を使う代わりにもっと適切です – Mrug
を使用してセルのインデックスパスを取得するには、 Yogeshメソッドを使用できますが、indexPathForItemAtPointのindexPathForRowAtPointを次のように変更してください:
CGPoint center= [sender center];
CGPoint rootViewPoint = [[sender superview] convertPoint:center toView:_tableView1];
NSIndexPath *indexPath = [_tableView1 indexPathForRowAtPoint:rootViewPoint];
NSLog(@"%@",indexPath);
- 1. テキストを取得してテーブルビューのセル内のテキストフィールドから保存する方法
- 2. テーブルビューのセル内のテキストフィールドからテキストを取得
- 3. テーブルビューのセルからテキストフィールドの値を取得する方法
- 4. グリッド内の各セルから項目を取得する方法
- 5. 現在のセルのindexPathを取得する方法
- 6. collectionViewセルのindexPathを取得する
- 7. iOS: `UICollectionview`、` indexPath`でセルの `contentoffset`を取得する方法
- 8. 選択したテーブルビューのセルからヘッダーセクションのタイトルを取得する方法は?
- 9. Webviewのキーボードから完了ボタンアクションを取得する方法
- 10. テーブルビュー機能の外でセルを取得する方法は?
- 11. セル内のDataGridViewButtonColumnの値を取得/設定する方法は?
- 12. HTMLテーブルのセルの内容を取得する方法は?
- 13. テーブルビューのセル内でUILabelの位置を変更する方法
- 14. コレクションビューのセル内のテーブルビュー
- 15. 'リセット'テーブルビューのセルの内容
- 16. テーブルビューのセル内のボタン
- 17. コレクションビュー内の2方向セル、セルタイトルを取得する方法?
- 18. gridControl内の特定のセルのコンボボックスを取得する方法
- 19. テーブルビューのセル数を取得する方法はありますか?
- 20. テーブルビューのセルでチェックボックスを取得する
- 21. IndexPathスウィフトテーブルビューは、私はテーブルビューでクリックされたボタンのindexPathを取得するには、次のコードでは、列
- 22. テーブルから特定のセルの内容を取得する
- 23. テーブルビュー内のすべてのセルを.gif初期化する方法は?
- 24. UISearchDisplayControllerからself.tableview indexpathを取得するtableView indexpath
- 25. Jqgridからセルの内容を取得する
- 26. データグリッドからセルの内容を取得するAutomationUI
- 27. セッション内のグラフから変数を取得する方法は?
- 28. Symfony:FormType内のルートからパラメータを取得する方法は?
- 29. コンテナ内の店舗からステートを取得する方法は?
- 30. コントローラ内のデータバインディングから1アイテムを取得する方法は?
可能な複製ate of [IOS 7 - UITableViewCellに配置されたボタンからindexPathを取得する方法](http://stackoverflow.com/questions/19000356/ios-7-how-to-get-the-indexpath-from-button-placed- in-uitableviewcell) –