UIGestureRecognizerとUITableViewCellの問題
-
25-09-2019 - |
質問
私はそうのようなUISwipeGestureRecognizer
方法でUITableViewCell
にcellForRowAtIndexPath:
を添付しています:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UISwipeGestureRecognizer *gesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(didSwipe:)];
gesture.direction = UISwipeGestureRecognizerDirectionRight;
[cell.contentView addGestureRecognizer:gesture];
[gesture release];
}
return cell;
}
しかし、didSwipe
メソッドは常に成功スワイプで二回呼ばなっています。私は当初、これはジェスチャーが開始および終了するのでだと思ったが、私はgestureRecognizer自体をログアウトした場合、彼らは「終了した」状態の両方であります:
-(void)didSwipe:(UIGestureRecognizer *)gestureRecognizer {
NSLog(@"did swipe called %@", gestureRecognizer);
}
コンソールます:
2011-01-05 12:57:43.478 App[20752:207] did swipe called <UISwipeGestureRecognizer: 0x5982fa0; state = Ended; view = <UITableViewCellContentView 0x5982c30>; target= <(action=didSwipe:, target=<RootViewController 0x5e3e080>)>; direction = right>
2011-01-05 12:57:43.480 App[20752:207] did swipe called <UISwipeGestureRecognizer: 0x5982fa0; state = Ended; view = <UITableViewCellContentView 0x5982c30>; target= <(action=didSwipe:, target=<RootViewController 0x5e3e080>)>; direction = right>
私は本当に理由を知りません。私は明らかにエンド状態をチェックしてみましたが、彼らの両方が、とにかく「エンド」としてでてくるよう、それは何の助けではありません...任意のアイデア?
解決
の代わりに、直接セルにジェスチャー認識を追加するのではviewDidLoad
でテーブルビューに追加することができます。
didSwipe
-法では、次のようにあなたが影響を受けたIndexPathとセルを決定することができます:
-(void)didSwipe:(UIGestureRecognizer *)gestureRecognizer {
if (gestureRecognizer.state == UIGestureRecognizerStateEnded) {
CGPoint swipeLocation = [gestureRecognizer locationInView:self.tableView];
NSIndexPath *swipedIndexPath = [self.tableView indexPathForRowAtPoint:swipeLocation];
UITableViewCell* swipedCell = [self.tableView cellForRowAtIndexPath:swipedIndexPath];
// ...
}
}
他のヒント
これは、アプリデリゲート
で動作します- (void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
{
// code
}
私はこの同じ問題を抱えていたし、テーブルビューの属性に「スクロールが有効」刻々と過ぎてそれを解決します。
それは私がスワイプジェスチャーの後の最初の応答しなくタップを得ることはありません、今を除き、他の方法でアプリケーションに影響を与えないように、私のテーブルビューには、スクロールは必要ありません。
はAwakeFromNib方法でジェスチャを追加すると問題なく動作します。
class TestCell: UITableViewCell {
override func awakeFromNib() {
super.awakeFromNib()
let panGesture = UIPanGestureRecognizer(target: self,
action: #selector(gestureAction))
addGestureRecognizer(panGesture)
}
@objc func gestureAction() {
print("gesture action")
}
}
所属していません StackOverflow