swipe to delete in a UITableView which is embeded in a UIScrollView

后端 未结 5 1534
猫巷女王i
猫巷女王i 2020-12-17 04:04

I\'ve encountered a problem the same as UIScrollview enable delete row by swipe
It is a tableView and another view work as subViews of a scrollView , and I can\'t enab

相关标签:
5条回答
  • 2020-12-17 04:26

    I know this thread is old, but here is the swift 4 version, working in iOS 11 for me (You would subclass UIScrollView):

    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRequireFailureOf otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        if (otherGestureRecognizer.view != nil && otherGestureRecognizer.view!.superview != nil) {
            return otherGestureRecognizer.view!.superview!.isKind(of: UITableView.self)
        }
    
        return false
    }
    
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        if (gestureRecognizer.state != .possible) {
            return true
        }
    
        return false
    }
    
    0 讨论(0)
  • 2020-12-17 04:38

    If , I'm not mistaken , the touches are consumed by the scrollview and the editing of the table is not happening because the table is not getting the touches. This can be resolved by subclassing the UIScrollView in order to snd the touches to the next responder too. So it's just a matter of overrwriting the touchesBegan, moved and ended. Will update the answer later today with the code needed as I am on the road now. Cheers!

    EDIT:

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        [self.nextResponder touchesBegan:touches withEvent:event];
    }
    
    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
    {
        if(!self.dragging)
        {
            [self.nextResponder touchesMoved:touches withEvent:event];
        }
    }
    
    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
    {
        [self.nextResponder touchesEnded:touches withEvent:event];
    }
    

    Just create a class that inherits from UIScrollView and in the implementation drop this code. This will make the scrollView to not swallow the touches, but pass them on. Obviously , when creating your scrollView use the class you just created instead of UIScrollView. Sorry for the delay. Hope this helps.

    Cheers!

    0 讨论(0)
  • 2020-12-17 04:38

    @THOR's answer is okay, but if your UITableView is in a UIScrollView, you probably have another UIView in there too. When you scroll up on the tableview, you accidentally slide over to the "other view".

    This will prevent the sliding, and allow you to swipe to delete.

    -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    if (gestureRecognizer.state != 0) {
            return YES;
    } else {
        return NO;
    }
    

    }

    0 讨论(0)
  • 2020-12-17 04:47

    I've successfully used

    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
    

    in a UIScrollView subclass containing the tableview to enable a UISwipeGestureRecognizer residing in the tableview to trigger instead of being swallowed by the "main" scrollview's gesture recognizers.

    0 讨论(0)
  • 2020-12-17 04:51

    You need to use custom subclass of UIScrollView. It should works with table views in horizontal scroll views:

    @interface MyCoolScrollView : UIScrollView
    
    @end
    
    @implementation MyCoolScrollView
    
    // Allows inner UITableView swipe-to-delete gesture
    - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRequireFailureOfGestureRecognizer:(nonnull UIGestureRecognizer *)otherGestureRecognizer
    {
        return [otherGestureRecognizer.view.superview isKindOfClass:[UITableView class]];
    }
    
    @end
    
    0 讨论(0)
提交回复
热议问题