【问题标题】:UIGestureRecognizer and UITableViewCell issueUIGestureRecognizer 和 UITableViewCell 问题
【发布时间】:2011-06-04 00:11:57
【问题描述】:

我在cellForRowAtIndexPath: 方法中将UISwipeGestureRecognizer 附加到UITableViewCell,如下所示:

- (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 方法总是在成功滑动时被调用两次。我最初认为这是因为手势开始和结束,但如果我注销手势识别器本身,它们都处于“结束”状态:

-(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 状态,但这无济于事,因为它们都以“Ended”的形式出现......有什么想法吗?

【问题讨论】:

    标签: ios iphone ipad uitableview uigesturerecognizer


    【解决方案1】:

    除了直接将手势识别器添加到单元格中,您可以将其添加到viewDidLoad中的tableview中。

    didSwipe-方法中可以确定受影响的IndexPath和cell如下:

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

    【讨论】:

    • 谢谢!这阻止了它两次射击! :)
    • 谢谢,这有帮助,但我看到了一些奇怪的东西 - 滑动后,如果我想选择行,我需要按两次。第一次什么都不做,第二次调用 didSelectRow...有人看到了吗?
    • @OdedBenDov 我相信它是由手势识别器引起的,当我添加点击手势识别器并忘记在文本字段的结束编辑方法(子视图)中删除表格视图单元格选择时,我有奇怪的行为表格视图单元格),也许这可以作为解决您问题的线索,更多细节here
    • 您可能应该在调用 cellForRowAtIndexPath 之前检查 swipedIndexPath 是否为 nil。在我的例子中,我输入了第三个(不存在的)单元格,它返回了一个 nil 的索引路径,然后产生了一个实际的 UITableViewCell(在最后一行)。
    【解决方案2】:

    它将与应用代理一起使用

    - (void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
    {
    
    // code
    
    }
    

    【讨论】:

      【解决方案3】:

      我遇到了同样的问题,并通过在表格视图属性中勾选“启用滚动”来解决它。

      我的表格视图不需要滚动,因此它不会以任何其他方式影响应用程序,除了现在我没有在滑动手势后获得第一次无响应的点击。

      【讨论】:

        【解决方案4】:

        在 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")
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-11-09
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多