UIGestureRecognizer and UITableViewCell issue

前端 未结 4 1582
囚心锁ツ
囚心锁ツ 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];
            // ...
      }
    }
    

提交回复
热议问题