iphone - 为什么我的函数被调用了两次?

标签 iphone objective-c ios uitableview

我有一个滑动识别器附加到我的表格 View 中的单元格

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

      //swipe recognition
        UISwipeGestureRecognizer *g = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(cellWasSwiped:)];
        [cell addGestureRecognizer:g];
        [g release];


    }


    // Configure the cell...
    [[cell textLabel] setText:[NSString stringWithFormat:@" number %d", indexPath.row]];
    return cell;
}

滑动功能是

- (void)cellWasSwiped:(UIGestureRecognizer *)g {
    NSLog(@"sunt in cellWasSwiped");
    swipeViewController *svc = [[swipeViewController alloc]init];
  [self.navigationController pushViewController:svc animated:YES];
  [svc release];
}

通过引入断点,我看到我的滑动功能被调用了 2 次,并且有两个相同的 View Controller 被推送到我的导航 Controller 上。为什么我滑动单元格时会调用两次滑动功能?

最佳答案

您的cellWasSwiped 可以在UIGestureRecognizer 状态改变时被多次调用。您需要检查属性 state,当用户完成他的操作时,它将是 UIGestureRecognizerStateEnded。检查状态 UIGestureRecognizerStateFailedUIGestureRecognizerStateCancelled 也很好。

关于iphone - 为什么我的函数被调用了两次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6622667/

相关文章:

iphone - 导航 Controller 内的选项卡栏 Controller ,或共享导航 Root View

objective-c - 从 C++ 循环启动 Cocoa GUI 并传递引用

iphone - 如何将 Xcode 3.1.2 升级到 Xcode 4.4.3

iphone - NSURLRequest 超时 IOS

iphone - 组合页面控件+ ScrollView (包括 ImageView )+ TextView

iphone - 如何使用 iPhone SDK 为这样的图像制作动画?

objective-c - 使用 dSYM 文件对故障转储进行符号化时,为什么 GDB 会显示 "no line number info available"?

ios - 从给定的字符串中仅检索正整数

ios - 无法在真正的 Apple Watch 上运行应用程序

ios - 正交tilemap cocos2d游戏在iPhone上运行时为什么会出现tile之间的空隙?