UIGestureRecognizer和UITableViewCell问题 [英] UIGestureRecognizer and UITableViewCell issue

查看:78
本文介绍了UIGestureRecognizer和UITableViewCell问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 UISwipeGestureRecognizer 附加到 cellForRowAtIndexPath中的 UITableViewCell 方法如下:

I am attaching a UISwipeGestureRecognizer to a UITableViewCell in the cellForRowAtIndexPath: method like so:

- (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本身,它们都处于结束状态:

However, the didSwipe method is always getting called twice on successful swipe. I initially thought this was because the gesture starts and ends, but if I log out the gestureRecognizer itself, they are both in the "Ended" state:

-(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>

我真的不知道为什么。我试着明确检查Ended状态,但这没有任何帮助,因为他们都以结束的方式进入......任何想法?

I really really don't know why. I tried obviously checking for the Ended state, but that is no help as they both come in as "Ended" anyway... Any ideas?

推荐答案

不是直接将手势识别器添加到单元格,而是可以在 viewDidLoad 中将其添加到tableview。

Instead of adding the gesture recognizer to the cell directly, you can add it to the tableview in viewDidLoad.

didSwipe -Method中,您可以按如下方式确定受影响的IndexPath和单元格:

In the didSwipe-Method you can determine the affected IndexPath and cell as follows:

-(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];
        // ...
  }
}

这篇关于UIGestureRecognizer和UITableViewCell问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆