UIGestureRecognizer and UITableViewCell issue

前端 未结 4 1570
囚心锁ツ
囚心锁ツ 2020-11-27 11:14

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

- (UITabl         


        
相关标签:
4条回答
  • 2020-11-27 11:51

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

    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];
            // ...
      }
    }
    
    0 讨论(0)
  • 2020-11-27 12:05

    It will work with app delegate

    - (void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath
    {
    
    // code
    
    }
    
    0 讨论(0)
  • 2020-11-27 12:14

    I had this same problem and solved it by ticking "Scrolling Enabled" in the table view attributes.

    My table view doesn't need scrolling, so it doesn't affect the app in any other way, except now I don't get the first unresponsive tap after a swipe gesture.

    0 讨论(0)
  • 2020-11-27 12:15

    Adding gesture in AwakeFromNib method works with no problems.

    class TestCell: UITableViewCell {
    
        override func awakeFromNib() {
            super.awakeFromNib()
    
            let panGesture = UIPanGestureRecognizer(target: self,
                                                action: #selector(gestureAction))
            addGestureRecognizer(panGesture)
        }
    
        @objc func gestureAction() {
            print("gesture action")
        }
    }
    
    0 讨论(0)
提交回复
热议问题