私は2つのアクションを含むテーブルビューを持っています.1つ目はdidSelectRowAtIndexPathデリゲートを使用し、2つ目はセルのボタンでアクションを使用して何か他のことをしたいと考えています。私の問題は、インデックスパスを取得できないことですか? それを行うためのベストプラクティスは何ですか。
9991 次
5 に答える
9
ボタンをセルに追加した場合、
[cell.contentView addSubview:button];
次に、次のようにインデックス パスを取得できます。
- (void)onButtonTapped:(UIButton *)button {
UITableViewCell *cell = (UITableViewCell *)button.superview.superview;
NSIndexPath *indexPath = [tableView indexPathForCell:cell];
// Go ahead
}
于 2011-09-17T04:27:40.720 に答える
2
私はこのソリューションを使用しています - ポイントを使用してセルのインデックスパスを取得しています
CGPoint center= [sender center];
CGPoint rootViewPoint = [[sender superview] convertPoint:center toView:_tableView1];
NSIndexPath *indexPath = [_tableView1 indexPathForRowAtPoint:rootViewPoint];
NSLog(@"%@",indexPath);
その仕事は完全に
于 2014-11-21T08:20:55.747 に答える
-1
ボタンが移動された場合、または 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];
}
}
于 2014-01-17T08:09:33.413 に答える